강의

멘토링

커뮤니티

Inflearn Community Q&A

owenchoi96's profile image
owenchoi96

asked

Data Analysis SQL Fundamentals

first_value and last_value Analytic SQL Practice

first_value() 와 row_number() 비교에 대한 질문

Written on

·

210

0

안녕하세요 선생님:) 좋은 강의 감사합니다.

다름이 아니라 first_value() 대신 row_number()를 통해 값을 구할 수 있나에 대해 생각하다가 질문을 드리게 되었습니다.

예제를 보면 

select empno, deptno, hiredate, sal
        , first_value(sal) over (partition by deptno order by hiredate) as first_hiredate_sal
from emp;

이 쿼리가 있었는데, row_number()를 사용하여 아래 처럼 쿼리를 작성하면 위에 쿼리와 동일한 값을 가질 수 있는 건가요?

 

with temp_01
as
(
select empno, deptno, hiredate, sal
       , row_number() over (partition by deptno order by hiredate) as rn_sal
)
select empno, deptno, hiredate, sal
from temp_01
where rn_sal = 1;
        

DBMS/RDBMSpostgresqlsql

Answer 1

1

dooleyz3525님의 프로필 이미지
dooleyz3525
Instructor

안녕하십니까, 

동일하지 않습니다. 적어주신대로 수행하면 rn_sal = 1 인것만 temp_01에서 추출하게 되므로 first_value()를 수행한것보다 건수가 더 작아지게 됩니다. 

감사합니다. 

owenchoi96's profile image
owenchoi96

asked

Ask a question