Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding solutions of the exercises - challenge 2 #49

Open
wants to merge 4 commits into
base: challenge-2
Choose a base branch
from

Conversation

franc1sc0sv
Copy link

No description provided.

README.md Outdated
Comment on lines 83 to 85
SELECT ac.type, SUM(ac.mount) AS total
FROM accounts ac
GROUP BY ac.type;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omit using the aliases since we're only using the columns from one table

README.md Outdated
Comment on lines 104 to 107
SELECT ac.id, ac.user_id, ac.type, ac.mount
FROM accounts ac
ORDER BY ac.mount DESC
LIMIT 5;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment

README.md Outdated
Comment on lines 117 to 122
WHEN m.type = 'IN' THEN m.mount
WHEN m.type = 'TRANSFER' AND m.account_to = a.id THEN m.mount
WHEN m.type = 'TRANSFER' AND m.account_from = a.id THEN -m.mount
WHEN m.type = 'OUT' THEN -m.mount
WHEN m.type = 'OTHER' AND m.account_from = a.id THEN -m.mount
WHEN m.type = 'OTHER' AND m.account_to = a.id THEN m.mount
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two common patterns, so we can reduce these to be 3 conditionals

README.md Outdated
FROM
accounts a
LEFT JOIN
movements m ON (m.account_from = a.id OR m.account_to = a.id)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can use IN instead of the OR to improve the readibility

README.md Outdated
Comment on lines 170 to 175
WHEN m.type = 'IN' THEN m.mount
WHEN m.type = 'TRANSFER' AND m.account_to = a.id THEN m.mount
WHEN m.type = 'TRANSFER' AND m.account_from = a.id THEN -m.mount
WHEN m.type = 'OUT' THEN -m.mount
WHEN m.type = 'OTHER' AND m.account_from = a.id THEN -m.mount
WHEN m.type = 'OTHER' AND m.account_to = a.id THEN m.mount
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment

README.md Outdated

f. Once the transaction is correct, make a commit
```
IF saldo_despues_transfer < 731823.56 THEN
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment

README.md Outdated
f. Once the transaction is correct, make a commit
```
IF saldo_despues_transfer < 731823.56 THEN
RAISE EXCEPTION 'Saldo insuficiente para el movimiento OUT de 731823.56';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment

README.md Outdated
Comment on lines 244 to 249
WHEN m.type = 'IN' THEN m.mount
WHEN m.type = 'TRANSFER' AND m.account_to = a.id THEN m.mount
WHEN m.type = 'TRANSFER' AND m.account_from = a.id THEN -m.mount
WHEN m.type = 'OUT' THEN -m.mount
WHEN m.type = 'OTHER' AND m.account_from = a.id THEN -m.mount
WHEN m.type = 'OTHER' AND m.account_to = a.id THEN m.mount
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as before

README.md Outdated
FROM
accounts a
LEFT JOIN
movements m ON (m.account_from = a.id OR m.account_to = a.id)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

README.md Outdated
Comment on lines 307 to 316
us.id AS user_id,
us.name AS user_name,
ac.id AS account_id,
ac.type AS account_type,
mov.id AS movement_id,
mov.type AS movement_type,
mov.mount,
mov.account_from,
mov.account_to,
mov.created_at
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we know who is the user we're looking for, we can omit the columns about it, and keep the columns about movements: SELECT mov.*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants