365 Days of Coding: Day 26
In my opinion, out of the three challenge sites I have tried, leetcode and sqlpad comes close to close. The only difference between them being that leetcode offers challenges for multiple languages and has an active user base that frequently shares their insights in the forums. Since, I have finished most of the challenges in leetcode except for a medium challenge, I am going to continue using sqlpad for now.
Todays’s challenge was to find out the unique counts of customers per year and month.
Table: rental
| col_name | col_type |
|---|---|
| rental_id | integer |
| rental_ts | timestamp with timezone |
| invetory_id | integer |
| customer_id | smallint |
| return_ts | timestamp with timezone |
| staff_id | smallint |
| last_update | timestamp with timezone |
Expected Solution:
| year | mon | uu_cnt |
|---|---|---|
| 2020 | 1 | 123 |
| 2020 | 2 | 456 |
| 2020 | 3 | 789 |
My Solution:
SELECT EXTRACT(YEAR FROM rental_ts) AS year,
EXTRACT(MONTH FROM rental_ts) AS mon, COUNT(DISTINCT customer_id) AS uu_cnt
FROM rental
GROUP BY year, mon;

Leave a comment