본문 바로가기

SQL

(40)
[SQL] LeetCode: 1164. Product Price at a Given Date Product Price at a Given Date - LeetCode 주어진 컬럼은 product_id, new_price, chage_date (제품의 가격의 변화가 기록된 테이블) 이걸 가지고 2019-08-16 이라는 특정 날짜의 제품별 가격을 출력하는 문제이다. 뭔가 간단한 거 같은데 해결하는 데 꽤 오랜 시간이 걸렸다. - 가격이 변화한 경우에만 데이터가 존재 - 가격 변화 전 모든 제품의 가격은 10 [풀이] -- 1. 출력: product_id, price -- 2. 필터: 2019-08-16의 가격 -- 3. 정렬: X WITH sub AS( SELECT product_id AS pid ,MAX(change_date) AS last_change_date FROM products WHER..
[SQL] LeetCode: 180. Consecutive Numbers 180. Consecutive Numbers LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com -- 1. 출력: 3번 이상 연속으로 나오는 숫자 -- 2. 필터: 3번 이상 연속 -- 3. 정렬: X SELECT DISTINCT num AS ConsecutiveNums FROM( SELECT * ,LAG(num,1) OVER (ORDER BY id) AS lag..
[SQL] LeetCode: 610. Triangle Judgement 610. Triangle Judgement LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com -- 1.출력: x,y,z, 삼각형 만들 수 있는지 여부 -- 2.필터: X -- 3.정렬: X SELECT * ,CASE WHEN x + y > z AND x + z > y AND y + z > x THEN 'Yes' ELSE 'No' END AS 'triangle' ..
[SQL] LeetCode : 184. Department Highest Salary 184. Department Highest Salary LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com -- 1. 출력: department name, employee name, salary -- 2. 필터: the highest salary in each of the departments -- 3. 정렬: X WITH max AS( SELECT e.name ..
[SQL] LeetCode: 1789. Primary Department for Each Employee 1789. Primary Department for Each Employee LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com [풀이1] - 처음 풀었던 풀이; Window 함수 이용해서 풀이함 -- 1. 출력: employee_id, department_id -- 2. 필터: 직원별 primary department, 한 부서 소속은 그 department 출..
[SQL] LeetCode: 1731. The Number of Employees Which Report to Each Employee 1731. The Number of Employees Which Report to Each Employee LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com [문제] For this problem, we will consider a manager an employee who has at least 1 other employee reporting to them. ..
[SQL] 프로그래머스: 조건별로 분류하여 주문상태 출력하기 프로그래머스 SQL 고득점 KIT String, Date- 조건별로 분류하여 주문상태 출력하기(Level3) 문제 FOOD_ORDER 테이블에서 5월 1일을 기준으로 주문 ID, 제품 ID, 출고일자, 출고여부를 조회하는 SQL문을 작성해주세요. 출고여부는 5월 1일까지 출고완료로 이 후 날짜는 출고 대기로 미정이면 출고미정으로 출력해주시고, 결과는 주문 ID를 기준으로 오름차순 정렬해주세요. 답안 -- 1.출력: 주문 ID, 제품 ID, 출고일자, 출고여부 -- (출고여부:5월 1일까지 출고완료/ 이 후 날짜는 출고 대기/ 미정이면 출고미정) -- 2.필터: 5월 1일을 기준 -- 3.정렬: 주문ID(ASC) SELECT order_id ,product_id ,DATE_FORMAT(out_date,'%..
[SQL] 요일 별, 시간 별 세션 수 출력(피봇테이블) 보호되어 있는 글입니다.