-
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
✨ DB Nerdery Challenges Solutions II #53
base: challenge-2
Are you sure you want to change the base?
Conversation
README.md
Outdated
@@ -60,7 +60,7 @@ create database nerdery_challenge; | |||
``` | |||
\q | |||
``` | |||
|
|||
C:\Users\arago\OneDrive\Escritorio\nerdery-repos\DB-Nerdery-Challenges\src\dump.sql |
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.
What does this mean?
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.
I was copying the dump.sql path and forgot to delete from readme file
README.md
Outdated
SELECT type, SUM(mount) AS total | ||
FROM accounts | ||
group by 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.
Nit: use uppercase for keywords
README.md
Outdated
|
||
2. How many users with at least 2 `CURRENT_ACCOUNT`. | ||
|
||
```sql | ||
SELECT count(u.name) as user_w_least_two_accounts |
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.
nit: use uppercase with keywords
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.
What it is going to happen if for any reason do you have a null value in u.name?
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.
This will only count u.name fields with values and ignore null fields
README.md
Outdated
DO | ||
$$ | ||
DECLARE | ||
account_balance RECORD; | ||
BEGIN | ||
FOR account_balance IN | ||
WITH account_balances AS (SELECT u.name, | ||
a.account_id, | ||
a.mount AS initial_mount, | ||
COALESCE(SUM( | ||
CASE | ||
WHEN m.type = 'TRANSFER' AND m.account_from = a.id | ||
THEN -m.mount | ||
WHEN m.type = 'TRANSFER' AND m.account_to = a.id | ||
THEN m.mount | ||
WHEN m.type = 'IN' AND m.account_from = a.id | ||
THEN m.mount | ||
WHEN m.type = 'OUT' AND m.account_from = a.id | ||
THEN -m.mount | ||
WHEN m.type = 'OTHER' AND m.account_from = a.id | ||
THEN m.mount | ||
ELSE 0 | ||
END | ||
), 0) AS movement_total | ||
FROM users AS u | ||
INNER JOIN accounts AS a ON u.id = a.user_id | ||
LEFT JOIN movements AS m ON a.id IN (m.account_from, m.account_to) | ||
GROUP BY u.id, u.name, a.account_id, a.mount) | ||
SELECT account_id, | ||
(initial_mount + movement_total) AS updated_balance | ||
FROM account_balances | ||
|
||
LOOP | ||
UPDATE accounts | ||
SET mount = account_balance.updated_balance | ||
WHERE account_id = account_balance.account_id; | ||
END LOOP; | ||
END | ||
$$; |
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.
I don't think this is the exercise it is mentioned
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.
I understand you point here, but to get the result was not needed to persist that information
README.md
Outdated
SELECT u.name || ' ' || u.last_name AS full_name, a.mount | ||
FROM users AS u | ||
INNER JOIN accounts AS a ON u.id = a.user_id | ||
ORDER BY a.mount DESC | ||
LIMIT 3; |
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.
This is not the actual result consider that you need to include the after movements from the movements table.
This is not considering the movements done
README.md
Outdated
```sql | ||
SELECT id, mount | ||
FROM accounts | ||
WHERE accounts.id = '3b79e403-c788-495a-a8ca-86ad7643afaf' OR accounts.id = 'fd244313-36e5-4a17-a27c-f8265bc46590'; |
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.
Suggestion: here you can directly use IN to get both ids filtered
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.
In other hand you need to get the result based on after all movements
README.md
Outdated
50.75) | ||
RETURNING * INTO movement; | ||
|
||
IF (SELECT mount FROM accounts WHERE id = movement.account_from) < movement.mount 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.
Bear in mind that you need to take a look over after movements values
README.md
Outdated
FOR updated_account_record IN | ||
UPDATE accounts | ||
SET mount = CASE | ||
WHEN id = movement.account_from THEN mount - movement.mount | ||
WHEN id = movement.account_to THEN mount + movement.mount | ||
END | ||
WHERE id = movement.account_from | ||
OR id = movement.account_to | ||
RETURNING * | ||
LOOP | ||
RAISE INFO 'Updated account %', updated_account_record.id; | ||
END LOOP; | ||
|
||
RAISE INFO 'Transaction successful'; | ||
COMMIT; | ||
END |
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.
I understand your point in here but this will change how the process is managed and will ignore the limit under the after make movements
UPDATE accounts | ||
SET mount = mount - record.mount | ||
WHERE id = record.account_from | ||
RETURNING * INTO accounts_record; | ||
|
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 as above
No description provided.