Skip to content

Commit d542c1e

Browse files
committed
islam added aggregate functions #[6,8]
1 parent 751c159 commit d542c1e

File tree

2 files changed

+206
-0
lines changed
  • Basic Aggregate Functions

2 files changed

+206
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [Monthly Transactions I](https://leetcode.com/problems/monthly-transactions-i/description/?envType=study-plan-v2&envId=top-sql-50)
2+
3+
### Problem Requirements:
4+
5+
Find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount.
6+
7+
Return the result table in any order.
8+
9+
<details>
10+
<summary style="font-size:1.3rem;"> <strong>Hints</strong> </summary>
11+
12+
<details>
13+
<summary>Hint#1</summary>
14+
<p>How can you do some things when you find given conditions?</p>
15+
</details>
16+
<details>
17+
<summary>Hint#2</summary>
18+
<p>Try it using <code>CASE</code> statement</p>
19+
</details>
20+
<details>
21+
<summary>Hint#3</summary>
22+
<p>
23+
SQL has an aggregation function called <code>COUNT(expression)</code> which count all the rows that satisfy a specified condition
24+
</p>
25+
</details>
26+
<details>
27+
<summary>Hint#4</summary>
28+
<p>
29+
SQL has an aggregation function called <code>SUM(expression)</code> which calculate the sum of values in a set
30+
</p>
31+
</details>
32+
<details>
33+
<summary>Hint#5</summary>
34+
<p>Use <code>GROUP BY</code> clause to group a set of rows into a set of summary rows</p>
35+
</details>
36+
37+
</details>
38+
39+
<details>
40+
<summary style="font-size:1.3rem;"> <strong>Explanation</strong> </summary>
41+
42+
<ul>
43+
<li>To count the total number of transactions we can use <code>COUNT()</code> function </li>
44+
<li>
45+
To count the total number of approved transactions we can use <code>COUNT()</code> function with the help of <code>CASE</code> statement as follows
46+
<br>
47+
WHEN state = approved THEN add <code>1</code> ELSE add <code>0</code>
48+
</li>
49+
<li>To sum the total amount we can use <code>SUM()</code> function </li>
50+
<li>
51+
To sum the total approved amount we can use <code>SUM()</code> function with the help of <code>CASE</code> statement as follows
52+
<br>
53+
WHEN state = approved THEN add <code>amount</code> ELSE add <code>0</code>
54+
</li>
55+
<li>
56+
All these functions should grouped by the combination of month and country with the <code>GROUP BY</code> clause.
57+
</li>
58+
</ul>
59+
60+
</details>
61+
62+
<details>
63+
<summary style="font-size:1.3rem"><strong> SQL Solution</strong> </summary>
64+
65+
66+
```sql
67+
SELECT
68+
DATE_FORMAT(trans_date, '%Y-%m') AS month,
69+
country,
70+
COUNT(state) AS trans_count,
71+
SUM(
72+
CASE WHEN state = 'approved' THEN 1 ELSE 0 END
73+
) AS approved_count,
74+
SUM(amount) AS trans_total_amount,
75+
SUM(
76+
CASE WHEN state = 'approved' THEN amount ELSE 0 END
77+
) AS approved_total_amount
78+
FROM
79+
Transactions
80+
GROUP BY
81+
month,
82+
country
83+
84+
-- we can count the number of approved transactions in another way.
85+
86+
-- COUNT(
87+
-- CASE WHEN state = 'approved' THEN 1 ELSE NULL END
88+
-- ) AS approved_count
89+
90+
-- COUNT function will ignore NULL.
91+
```
92+
93+
</details>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# [Game Play Analysis IV](https://leetcode.com/problems/game-play-analysis-iv/description/?envType=study-plan-v2&envId=top-sql-50)
2+
3+
### Problem Requirements:
4+
5+
Find the number of players that logged in for at least two consecutive days starting from their first login date, then divide that number by the total number of players.
6+
7+
<details>
8+
<summary style="font-size:1.3rem;"> <strong>Hints</strong> </summary>
9+
10+
<details>
11+
<summary>Hint#1</summary>
12+
<p>
13+
What if you have another column that has the <code>first_login</code>,
14+
The problem will be very easy, isn't it?
15+
</p>
16+
</details>
17+
<details>
18+
<summary>Hint#2</summary>
19+
<p>
20+
Try use <code>WITH</code> clause. It will help you to define a temporary data set. It will make your life easier.
21+
</p>
22+
</details>
23+
<details>
24+
<summary>Hint#3</summary>
25+
<p>
26+
The SQL <code>MIN(expression)</code> function returns the minimum value in a set of values
27+
</p>
28+
</details>
29+
<details>
30+
<summary>Hint#4</summary>
31+
<p>
32+
Try use <code>window function</code>.
33+
</p>
34+
</details>
35+
36+
<details>
37+
<summary>Hint#5</summary>
38+
<p>SQL has an aggregation function called <code>COUNT(expression)</code> which count all the rows that satisfy a specified condition</p>
39+
</details>
40+
<details>
41+
<summary>Hint#6</summary>
42+
<p>
43+
To subtract an interval e.g., a year, a month and a day to date, you can use the <code>DATE_SUB()</code> function.
44+
</p>
45+
</details>
46+
<details>
47+
<summary>Hint#7</summary>
48+
<p>SQL has a <code>ROUND(number, decimals)</code> function which rounds a number to a specified number of decimal places. </p>
49+
</details>
50+
</details>
51+
52+
<details>
53+
<summary style="font-size:1.3rem;"> <strong>Explanation</strong> </summary>
54+
55+
We want to count the number of players who logged in the next day of their first login day.
56+
<br>
57+
we should determine the first login date for each player.
58+
Then when we find a player logged in the next day of this day we should put this player into account.
59+
60+
<ul>
61+
<li>We can determine the first login for each player with the help of aggregate window function
62+
<code>MIN(expression) OVER </code>
63+
. Aggregate window function will work the same as the normal aggregate function but it doesn't cause rows to become grouped into a single output row.
64+
</li>
65+
<li>To be able to use our original table and the new column we can use <code> WITH</code> clause , it will help us to define the temporary data set and use them in the query.
66+
</li>
67+
<li>
68+
We will filter this table according to our condition <code>(event_date and first_login are consecutive days)</code>.
69+
<br>
70+
We can use <code>DATE_SUB()</code> function to subtract a given duration from <code>event_date</code> As follows<br> <code>DATE_SUB(event_date, INTERVAL 1 DAY) = first_login</code>
71+
</li>
72+
<li>
73+
Finally, divide the player that satisfies the condition by the total number of player that we can calculate as a <code>subquery</code> then round the result using <code>ROUND()</code>function to round the result.
74+
</li>
75+
</ul>
76+
77+
</details>
78+
79+
<details>
80+
<summary style="font-size:1.3rem"><strong> SQL Solution</strong> </summary>
81+
82+
83+
```sql
84+
WITH temp AS(
85+
SELECT
86+
*,
87+
MIN(event_date) OVER (Partition BY player_id) AS first_login
88+
-- over means that the function before it is a window function
89+
FROM
90+
Activity
91+
)
92+
SELECT
93+
ROUND(
94+
(
95+
SELECT
96+
COUNT(player_id)
97+
FROM
98+
temp
99+
WHERE
100+
DATE_SUB(event_date, INTERVAL 1 DAY) = first_login
101+
) / (
102+
SELECT
103+
COUNT(DISTINCT player_id)
104+
FROM
105+
Activity
106+
),
107+
2
108+
) AS fraction
109+
110+
111+
```
112+
113+
</details>

0 commit comments

Comments
 (0)