-
Notifications
You must be signed in to change notification settings - Fork 33
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
base: challenge-2
Are you sure you want to change the base?
Conversation
README.md
Outdated
SELECT ac.type, SUM(ac.mount) AS total | ||
FROM accounts ac | ||
GROUP BY ac.type; |
There was a problem hiding this comment.
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
SELECT ac.id, ac.user_id, ac.type, ac.mount | ||
FROM accounts ac | ||
ORDER BY ac.mount DESC | ||
LIMIT 5; |
There was a problem hiding this comment.
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
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
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 |
There was a problem hiding this comment.
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.*
No description provided.