Skip to content

Commit 16393a4

Browse files
Added Onboarded Cities
1 parent 85c0637 commit 16393a4

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

Onboarded Cities/READme.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Onboarded Cities
2+
![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99)
3+
[![View Main Folder](https://img.shields.io/badge/View-Main_Folder-971901?)](https://github.com/thecoddiwompler/SQL-Practice-Questions/tree/main)
4+
[![View Repositories](https://img.shields.io/badge/View-My_Repositories-blue?logo=GitHub)](https://github.com/thecoddiwompler?tab=repositories)
5+
[![View My Profile](https://img.shields.io/badge/View-My_Profile-green?logo=GitHub)](https://github.com/thecoddiwompler)
6+
7+
---
8+
## 🛠️ Problem Statement
9+
10+
<b>Table Name : business_city</b>
11+
12+
| Column Name |Type |
13+
| ------------- | ------------- |
14+
| business_date | date |
15+
| city_id | int |
16+
17+
</br>
18+
19+
The table contains two columns business_date and city_id. city_id is the ID of the city which has been onboarded for the business. Note that there could be multiple instances of onboarding of a particular city.
20+
21+
Write a SQL query to find the Unique number of cities onboarded for the business for the first time for each year.
22+
23+
Return the result table in the order of business_year.
24+
25+
The query result format is in the following example.
26+
27+
<details>
28+
<summary>
29+
Input
30+
</summary>
31+
</br>
32+
33+
<b> Table Name: business_city </b></br>
34+
35+
| business_year | city_id |
36+
|---------------|---------|
37+
| 2020-01-02 | 3 |
38+
| 2020-07-01 | 7 |
39+
| 2021-01-01 | 3 |
40+
| 2021-02-03 | 19 |
41+
| 2022-12-01 | 3 |
42+
| 2022-12-15 | 3 |
43+
| 2022-02-28 | 12 |
44+
45+
46+
47+
</details>
48+
49+
<details>
50+
<summary>
51+
Output
52+
</summary>
53+
</br>
54+
55+
| business_city | no_of_onboarded_cities_for_the_first_time |
56+
|-------------------------|----------|
57+
| 2020 | 2 |
58+
| 2021 | 1 |
59+
| 2022 | 1 |
60+
61+
62+
</details>
63+
64+
---

Onboarded Cities/schema.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CREATE TABLE business_city (business_date date, city_id int);
2+
3+
INSERT INTO
4+
business_city
5+
VALUES
6+
(cast('2020-01-02' AS date), 3),
7+
(cast('2020-07-01' AS date), 7),
8+
(cast('2021-01-01' AS date), 3),
9+
(cast('2021-02-03' AS date), 19),
10+
(cast('2022-12-01' AS date), 3),
11+
(cast('2022-12-15' AS date), 3),
12+
(cast('2022-02-28' AS date), 12);
13+
14+
COMMIT;

Onboarded Cities/solution.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
WITH cte AS (
2+
SELECT
3+
TO_CHAR(business_date, 'YYYY') business_year,
4+
city_id,
5+
ROW_NUMBER() OVER(
6+
PARTITION by city_id
7+
ORDER BY
8+
to_char(business_date, 'YYYY')
9+
) rn
10+
FROM
11+
business_city
12+
)
13+
SELECT
14+
business_year,
15+
COUNT(*) no_of_onboarded_cities_for_the_first_time
16+
FROM
17+
cte
18+
WHERE
19+
rn = 1
20+
GROUP BY
21+
1

iqsql.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
9. Manager with at least 5 direct reportees
1010
10. Mismatched IDs
1111
11. Odd and Even Measurements
12-
12. Qualifying Criteria
12+
12. Onboarded Cities
13+
13. Qualifying Criteria

0 commit comments

Comments
 (0)