Skip to content

Commit b8386c3

Browse files
committed
task: #1158
1 parent 6e02002 commit b8386c3

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Leetcode (PostgreSQL)
22

3+
![Postgres](https://img.shields.io/badge/postgres-%23316192.svg?style=for-the-badge&logo=postgresql&logoColor=white)
34
[![MIT licensed](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE)
45
[![CI](https://github.com/ivanbyone/leetcode-sql/actions/workflows/ci.yml/badge.svg)](https://github.com/Ivanbyone/leetcode-sql//actions)
5-
![Postgres](https://img.shields.io/badge/postgres-%23316192.svg?style=for-the-badge&logo=postgresql&logoColor=white)
66

77
## Description
88

@@ -46,6 +46,7 @@ Have a good contributing!
4646
2. [Medium](./leetcode/medium/)
4747
- [176. Second Highest Salary](./leetcode/medium/176.%20Second%20Highest%20Salary.sql)
4848
- [184. Department Highest Salary](./leetcode/medium/184.%20Department%20Highest%20Salary.sql)
49+
- [1158. Market Analysis 1](./leetcode/medium/1158.%20Market%20Analysis%201.sql)
4950

5051
## License
5152

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Question 1158. Market Analysis 1
3+
Link: https://leetcode.com/problems/market-analysis-i/description/
4+
5+
Table: Users
6+
7+
+----------------+---------+
8+
| Column Name | Type |
9+
+----------------+---------+
10+
| user_id | int |
11+
| join_date | date |
12+
| favorite_brand | varchar |
13+
+----------------+---------+
14+
user_id is the primary key (column with unique values) of this table.
15+
This table has the info of the users of an online shopping website where users can sell and buy items.
16+
17+
18+
Table: Orders
19+
20+
+---------------+---------+
21+
| Column Name | Type |
22+
+---------------+---------+
23+
| order_id | int |
24+
| order_date | date |
25+
| item_id | int |
26+
| buyer_id | int |
27+
| seller_id | int |
28+
+---------------+---------+
29+
order_id is the primary key (column with unique values) of this table.
30+
item_id is a foreign key (reference column) to the Items table.
31+
buyer_id and seller_id are foreign keys to the Users table.
32+
33+
34+
Table: Items
35+
36+
+---------------+---------+
37+
| Column Name | Type |
38+
+---------------+---------+
39+
| item_id | int |
40+
| item_brand | varchar |
41+
+---------------+---------+
42+
item_id is the primary key (column with unique values) of this table.
43+
44+
45+
Write a solution to find for each user, the join date and the number of orders they made as a buyer in 2019.
46+
47+
Return the result table in any order.
48+
*/
49+
50+
SELECT
51+
u.user_id AS buyer_id,
52+
u.join_date,
53+
COUNT(o.order_id) AS orders_in_2019
54+
FROM Users AS u
55+
LEFT JOIN
56+
Orders AS o
57+
ON
58+
u.user_id = o.buyer_id
59+
AND o.order_date BETWEEN '2019-01-01' AND '2019-12-31'
60+
GROUP BY u.user_id, u.join_date

0 commit comments

Comments
 (0)