1158. Market Analysis I
https://leetcode.com/problems/market-analysis-i/
Market Analysis I - LeetCode
Can you solve this real interview question? Market Analysis I - Table: Users +----------------+---------+ | Column Name | Type | +----------------+---------+ | user_id | int | | join_date | date | | favorite_brand | varchar | +----------------+---------+ u
leetcode.com
Write an SQL query to find for each user, the join date and the number of orders they made as a buyer in 2019.
Return the result table in any order.
# 1
SELECT U.user_id AS buyer_id
, join_date
, COUNT(CASE WHEN YEAR(order_date) = 2019
THEN 1
ELSE NULL END) AS orders_in_2019
FROM Users U LEFT JOIN Orders O ON U.user_id = O.buyer_id
GROUP BY user_id, join_date
# 2
SELECT U.user_id AS buyer_id
, join_date
, IFNULL(COUNT(order_date), 0) AS orders_in_2019
FROM Users as U LEFT JOIN Orders as O
ON U.user_id = O.buyer_id
AND YEAR(order_date) = '2019'
GROUP BY U.user_id
'🛠️Skill > CodingTest' 카테고리의 다른 글
[leetcode] 1164. Product Price at a Given Date / 지정 날짜에 변경된 데이터 조회 / UNION (0) | 2023.02.17 |
---|---|
[leetcode] 577. Employee Bonus / COALESCE (0) | 2023.01.15 |
[프로그래머스] Lv4. 저자 별 카테고리 별 매출액 집계하기 (0) | 2022.12.18 |
[프로그래머스] Lv2.조건에 맞는 도서와 저자 리스트 출력/Lv3. 카테고리 별 도서 판매량 집계 (0) | 2022.12.16 |
[프로그래머스] LV4. 자동차 대여 기록 별 대여 금액 구하기 / DATEDIFF (0) | 2022.12.14 |
댓글