leetcode2 [Leetcode/SQL] 1907. Count Salary Categories https://leetcode.com/problems/count-salary-categories/description/?envType=study-plan-v2&envId=top-sql-50문제 설명Accounts 테이블에서 income 액수별로 카테고리를 나누어 그 카테고리에 해당하는 계좌의 수를 출력하라. 최종 쿼리 select 'Low Salary' as Category ,sum(income =20000 and income 50000) as accounts_countfrom accounts 쿼리 설명 및 느낀 점처음엔 문제를 보고 case when과 group by를 사용해서 카테고리별로 해당하는 계좌의 수를 출력하면 되는구나 생각을 했지만 Average Salary 조건에 해당하는 계좌의.. 2024. 8. 20. [Leetcode/SQL] 197.Rising Temperature 문제어제 기온과 비교해서 기온이 높은 날짜만 id를 출력해보셈최종쿼리with temp as(select *, lag(temperature) over(order by recordDate) as pre_tem, lag(recordDate) over(order by recordDate) as pre_datefrom weather )select idfrom temp where temperature >pre_temand datediff(recordDate,pre_date) =1;쿼리 설명 + 느낀점어제 기온을 비교하기 위해 어제의 날짜와 어제의 기온 컬럼을 가져와야 하는데 현재 행 기준 이전 행을 가져오는 lag 윈도우 함수가 제일 먼저 떠올랐다. with문과 lag 윈도우 함수를 활용해.. 2024. 8. 19. 이전 1 다음