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

✨ DB Nerdery Challenges Solutions II #53

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

Conversation

marvnramos
Copy link

No description provided.

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
Copy link

Choose a reason for hiding this comment

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

What does this mean?

Copy link
Author

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
Comment on lines 82 to 84
SELECT type, SUM(mount) AS total
FROM accounts
group by type
Copy link

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
Copy link

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

Copy link

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?

Copy link
Author

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
Comment on lines 118 to 156
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
$$;
Copy link

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

Copy link

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
Comment on lines 158 to 162
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;
Copy link

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';
Copy link

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

Copy link

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
Copy link

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
Comment on lines 205 to 220
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
Copy link

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

Comment on lines +243 to +247
UPDATE accounts
SET mount = mount - record.mount
WHERE id = record.account_from
RETURNING * INTO accounts_record;

Copy link

Choose a reason for hiding this comment

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

Same as above

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