본문 바로가기

group by13

[leetcode / MySQL] 1158. Market Analysis I 문제 💡 Write a solution 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. 테이블 형태 풀이 SELECT user_id AS buyer_id, join_date, SUM(CASE WHEN YEAR(order_date)=2019 THEN 1 ELSE 0 END) AS orders_in_2019 FROM Users AS u LEFT JOIN Orders AS o ON u.user_id=o.buyer_id GROUP BY user_id 해설 각 유저별 가입 일자와 2019년에 buyer로서 주문한 order 데이터의.. 2024. 2. 27.
[leetcode / MySQL] 1070. Product Sales Analysis III 문제 💡 Write a solution to select the product id, year, quantity, and price for the first year of every product sold. Return the resulting table in any order. 테이블 형태 풀이 SELECT product_id, year AS first_year, quantity, price FROM Sales WHERE (product_id, year) IN (SELECT product_id, MIN(year) AS year FROM Sales GROUP BY product_id) 해설 SELECT product_id, year AS first_year, quantity, price FROM Sale.. 2024. 2. 27.
[leetcode / MySQL] 585. Investments in 2016 문제 💡 Write a solution to report the sum of all total investment values in 2016 tiv_2016, for all policyholders who: have the same tiv_2015 value as one or more other policyholders, and are not located in the same city as any other policyholder (i.e., the (lat, lon) attribute pairs must be unique). Round tiv_2016 to two decimal places. 테이블 형태 풀이 SELECT ROUND(SUM(tiv_2016),2) AS tiv_2016 FROM Insu.. 2024. 2. 22.
[프로그래머스 / MySQL] 진료과별 총 예약 횟수 출력하기 문제 💡 APPOINTMENT 테이블에서 2022년 5월에 예약한 환자 수를 진료과코드 별로 조회하는 SQL문을 작성해주세요. 이때, 컬럼명은 '진료과 코드', '5월예약건수'로 지정해주시고 결과는 진료과별 예약한 환자 수를 기준으로 오름차순 정렬하고, 예약한 환자 수가 같다면 진료과 코드를 기준으로 오름차순 정렬해주세요. 테이블 형태 풀이 SELECT MCDP_CD AS 진료과코드, COUNT(PT_NO) AS 5월예약건수 FROM APPOINTMENT WHERE YEAR(APNT_YMD) = 2022 AND MONTH(APNT_YMD) = 05 GROUP BY MCDP_CD ORDER BY 5월예약건수, 진료과코드 해설 FROM APPOINTMENT WHERE YEAR(APNT_YMD) = 2022 .. 2024. 2. 2.
[leetcode / MySQL] 262. Trips and Users 문제 💡 The cancellation rate is computed by dividing the number of canceled (by client or driver) requests with unbanned users by the total number of requests with unbanned users on that day. Write a solution to find the cancellation rate of requests with unbanned users (both client and driver must not be banned) each day between "2013-10-01" and "2013-10-03". Round Cancellation Rate to two decima.. 2024. 1. 31.
[leetcode / MySQL] 570. Managers with at Least 5 Direct Reports 문제 💡 Write a solution to find managers with at least five direct reports. Return the result table in any order. 테이블 형태 풀이 SELECT e2.name FROM Employee e1 INNER JOIN Employee e2 ON e1.managerId=e2.id GROUP BY e1.managerId HAVING COUNT(DISTINCT e1.id) >= 5 해설 SELECT e2.name FROM Employee e1 INNER JOIN Employee e2 ON e1.managerId=e2.id GROUP BY e1.managerId HAVING COUNT(DISTINCT e1.id) >= 5 최소 5명 이.. 2024. 1. 31.
[프로그래머스 / MySQL] 상품 별 오프라인 매출 구하기 문제 💡 PRODUCT 테이블과 OFFLINE_SALE 테이블에서 상품코드 별 매출액(판매가 * 판매량) 합계를 출력하는 SQL문을 작성해주세요. 결과는 매출액을 기준으로 내림차순 정렬해주시고 매출액이 같다면 상품코드를 기준으로 오름차순 정렬해주세요. 테이블 형태 풀이 SELECT PRODUCT_CODE, SUM(PRICE * SALES_AMOUNT) AS SALES FROM PRODUCT AS P JOIN OFFLINE_SALE AS O ON P.PRODUCT_ID = O.PRODUCT_ID GROUP BY PRODUCT_CODE ORDER BY SALES DESC, PRODUCT_CODE 해설 FROM PRODUCT AS P JOIN OFFLINE_SALE AS O ON P.PRODUCT_ID = O.. 2024. 1. 31.
[프로그래머스 / MySQL] 동명 동물 수 찾기 문제 💡 동물 보호소에 들어온 동물 이름 중 두 번 이상 쓰인 이름과 해당 이름이 쓰인 횟수를 조회하는 SQL문을 작성해주세요. 이때 결과는 이름이 없는 동물은 집계에서 제외하며, 결과는 이름 순으로 조회해주세요. 테이블 형태 풀이 SELECT NAME, COUNT(*) AS COUNT FROM ANIMAL_INS WHERE NAME IS NOT NULL GROUP BY NAME HAVING COUNT >= 2 ORDER BY NAME 해설 FROM ANIMAL_INS WHERE NAME IS NOT NULL 두 번 이상 쓰인 이름과 사용 횟수를 출력하는 문제다 이름이 NULL 인 값은 집계에서 제외해야 하므로 WHERE 절에서 IS NOT NULL 조건을 설정했다(중요) SELECT NAME, COUN.. 2024. 1. 31.
[leetcode / MySQL] 1045. Customers Who Bought All Products 문제 💡 Write a solution to report the customer ids from the Customer table that bought all the products in the Product table. Return the result table in any order. 테이블 형태 풀이 SELECT customer_id FROM Customer GROUP BY customer_id HAVING COUNT(DISTINCT product_key) = (SELECT COUNT(*) FROM Product) 해설 SELECT customer_id FROM Customer GROUP BY customer_id HAVING COUNT(DISTINCT product_key) = (SELECT CO.. 2024. 1. 30.