본문 바로가기
SQL

leetcode https://leetcode.com/problems/last-person-to-fit-in-the-bus/

by 무적김두칠 2023. 4. 21.

https://leetcode.com/problems/last-person-to-fit-in-the-bus/

 

Last Person to Fit in the Bus - LeetCode

Can you solve this real interview question? Last Person to Fit in the Bus - 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
2
3
4
5
6
7
8
SELECT person_name
FROM (
  SELECT person_id, person_name, turn, SUM(weight) OVER(ORDER BY turn) AS Total_Weight
  FROM Queue
) sub
WHERE Total_Weight <= 1000
ORDER BY Total_Weight desc
limit 1
cs

sum over를 하기때문에 이 누적값을 기준으로 조건을 걸면 

You cannot use the alias 'Total_Weight' of an expression containing a window function in this context.'
아마 위와 같은 에러가 나오게 됩니다.

따라서 서브쿼리 생성후 조건을 걸어주면 됩니다!

if the condition is based on this cumulative value,

You cannot use the alias 'Total_Weight' of an expression containing a window function in this context.'
You will probably get an error like the one above.

Therefore, after creating the sub-query, you can apply the condition!

반응형

댓글