8000 comments · dana3601/Leetcode-Problems@35fb3b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 35fb3b4

Browse files
committed
comments
1 parent 1d7b687 commit 35fb3b4

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

Customers or Users/# 1555. Bank Account Summary.sql

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,31 @@
33
# Find out the current balance of all users and
44
# check wheter they have breached their credit limit (If their current credit is less than 0).
55

6+
7+
8+
# First sulution is SUM the two types transaction in SELECT statement
9+
SELECT user_id,user_name,
10+
11+
# use IFNULL( ,0) because it could be a situation that the customer has no transaction
12+
IFNULL(SUM(CASE WHEN a.user_id=b.paid_by THEN -amount ELSE amount END),0)+a.credit as credit,
13+
CASE WHEN IFNULL(SUM(CASE WHEN a.user_id=b.paid_by THEN -amount ELSE amount END),0)>=-a.credit THEN "No" ELSE "Yes" END as credit_limit_breached
14+
FROM Users as a
15+
LEFT JOIN Transactions as b
16+
17+
# Becareful there is no user_id in Transactions table
18+
ON a.user_id=b.paid_by OR a.user_id=b.paid_to
19+
GROUP BY a.user_id;
20+
21+
22+
23+
24+
25+
# Second sulution is SUM the two types transaction by making two extra tables
626
SELECT u.user_id AS USER_ID,
727
u.user_name AS USER_NAME,
828
(u.credit - IFNULL(a.c_munis,0) + IFNULL(b.c_plus,0) )AS CREDIT,
29+
30+
# 'Yes' means excessed (breached their credit limit)
931
IF(u.credit - IFNULL(a.c_munis,0) + IFNULL(b.c_plus,0)<0,'Yes','No') AS CREDIT_LIMIT_BREACHED
1032
FROM users u
1133
LEFT JOIN
@@ -22,14 +44,6 @@ ON b.paid_to = u.user_id
2244

2345

2446

25-
SELECT user_id,user_name,
26-
IFNULL(SUM(CASE WHEN a.user_id=b.paid_by THEN -amount ELSE amount END),0)+a.credit as credit,
27-
CASE WHEN IFNULL(SUM(CASE WHEN a.user_id=b.paid_by THEN -amount ELSE amount END),0)>=-a.credit THEN "No" ELSE "Yes" END as credit_limit_breached
28-
FROM Users as a
29-
LEFT JOIN Transactions as b
30-
ON a.user_id=b.paid_by OR a.user_id=b.paid_to
31-
GROUP BY a.user_id;
32-
3347

3448

3549
with spent as (
@@ -57,6 +71,8 @@ order by user_id
5771

5872

5973

74+
75+
6076
with cte as
6177
(select q.user_id, sum(q.net_amt) as trans_total from
6278
(select paid_by as user_id, -1*sum(amount) as net_amt

Order and Deliver/# 1445. Apples & Oranges.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
# Report the difference between number of apples and oranges sold each day
44
# Return the result table ordered by sale_date in format ('YYYY-MM-DD')
55

6+
7+
# If there only two possible situations in CASE statement,
8+
# I can use ELSE for another situation, instead of stating both of two situations.
69
SELECT sale_date,
710
SUM(CASE WHEN fruit = "apples" THEN sold_num ELSE (-sold_num) END) AS diff
811
FROM Sales
12+
13+
# Don't forgot to GROUP BY when using SUM
914
GROUP BY sale_date
1015

1116

1217

18+
19+
1320
SELECT a.sale_date sale_date, num_apples - num_oranges as diff
1421
FROM
1522
# Create table for Apples
@@ -63,6 +70,14 @@ on a.sale_date=o.sale_date
6370

6471

6572

73+
with tmp as
74+
(select sale_date,
75+
sum(case when fruit = 'apples' then sold_num end) as apples,
76+
sum(case when fruit = 'oranges' then sold_num end) as oranges
77+
from sales
78+
group by sale_date)
79+
select sale_Date, (apples-oranges) as diff
80+
from tmp
6681

6782

6883

0 commit comments

Comments
 (0)
0