Skip to content

Commit 3b5dda3

Browse files
committed
Upload 172
1 parent 1498e84 commit 3b5dda3

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- markdownlint-disable MD041 -->
22
[![eevveerryyddaayy release](https://img.shields.io/badge/eevveerryyddaayy-v1.3.0-blue.svg)](https://github.com/ggeerraarrdd/eevveerryyddaayy/)
3-
[![Solved](https://img.shields.io/badge/solved-171-green.svg)](#index)
3+
[![Solved](https://img.shields.io/badge/solved-172-green.svg)](#index)
44
<!-- markdownlint-enable MD041 -->
55

66
# SQL Everyday
@@ -205,6 +205,7 @@ Because this project necessitated a framework to enable consistent daily practic
205205
| 169 | [NPV Queries](https://leetcode.com/problems/npv-queries/description/) | [Solution](solutions/169_npv_queries.md) | LeetCode | Easy | |
206206
| 170 | [Game Play Analysis II](https://leetcode.com/problems/game-play-analysis-ii/description/) | [Solution](solutions/170_game_play_analysis_ii.md) | LeetCode | Easy | |
207207
| 171 | [Total Traveled Distance](https://leetcode.com/problems/total-traveled-distance/description/) | [Solution](solutions/171_total_traveled_distance.md) | LeetCode | Easy | |
208+
| 172 | [Create a Session Bar Chart](https://leetcode.com/problems/create-a-session-bar-chart/description/) | [Solution](solutions/172_create_a_session_bar_chart.md) | LeetCode | Easy | Histogram bins w/ `UNION`, `CASE` |
208209
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
209210
<!--- cSpell:enable --->
210211

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# SQL Everyday \#172
2+
3+
## Create a Session Bar Chart
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Easy
7+
8+
## Problem
9+
10+
You want to know how long a user visits your application. You decided to create bins of `"[0-5>"`, `"[5-10>"`, `"[10-15>"`, and `"15 minutes or more"` and count the number of sessions on it.
11+
12+
Write a solution to report the `(bin, total)`.
13+
14+
Return the result table in *any order*. [[Full Description](https://leetcode.com/problems/create-a-session-bar-chart/description/)]
15+
16+
## Submitted Solution
17+
18+
```sql
19+
-- Submitted Solution
20+
WITH cte1 AS (
21+
SELECT '[0-5>' AS bin FROM Sessions
22+
UNION
23+
SELECT '[5-10>' AS bin FROM Sessions
24+
UNION
25+
SELECT '[10-15>' AS bin FROM Sessions
26+
UNION
27+
SELECT '15 or more' AS bin FROM Sessions
28+
),
29+
cte2 AS (
30+
SELECT
31+
CASE WHEN (duration/60) < 5 THEN '[0-5>'
32+
WHEN (duration/60) < 10 THEN '[5-10>'
33+
WHEN (duration/60) < 15 THEN '[10-15>'
34+
ELSE '15 or more' END AS bin
35+
FROM Sessions
36+
)
37+
SELECT
38+
c1.bin
39+
,COALESCE(COUNT(c2.bin), 0) AS total
40+
FROM cte1 AS c1
41+
LEFT JOIN cte2 AS c2 ON c1.bin = c2.bin
42+
GROUP BY c1.bin
43+
;
44+
```
45+
46+
## Site Solution
47+
48+
```sql
49+
-- LeetCode Solution
50+
-- None
51+
```
52+
53+
## Notes
54+
55+
This is easy enough with `CASE` and `GROUP BY`. But the not-so easy part is ensuring any missing bin is also counted as 0.
56+
57+
## NB
58+
59+
Histogram bins w/ `UNION`, `CASE`
60+
61+
Go to [Index](../?tab=readme-ov-file#index)\
62+
Go to [Overview](../?tab=readme-ov-file)

0 commit comments

Comments
 (0)