lead
lead
정렬된 프레임 안에서 현재 행보다 오프셋(row)만큼 뒤의 행에서 평가된 값을 반환하는 윈도우 함수예요.
이 함수는 leadInFrame와 비슷하지만, 항상 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING 프레임을 사용합니다.
출처: 문서
본문
구문(Syntax)
lead(x[, offset[, default]])
OVER ([[PARTITION BY grouping_column] [ORDER BY sorting_column]] | [window_name])
FROM table_name
WINDOW window_name as ([[PARTITION BY grouping_column] [ORDER BY sorting_column])
윈도우 함수 구문에 대한 자세한 내용은 윈도우 함수 - 구문을 참고하세요.
인자(Parameters)
x— 컬럼 이름.offset— 적용할 오프셋. (U)Int*. (선택, 기본값1).default— 계산된 행이 윈도우 프레임 경계를 벗어날 때 반환할 값. (선택, 생략하면 컬럼 타입의 기본값).
반환 값(Returned value)
- 정렬된 프레임 안에서 현재 행보다 오프셋(row)만큼 뒤의 행에서 평가된 값.
예시(Example)
이 예시는 노벨상 수상자들의 역사 데이터를 보면서 lead 함수로 물리학 분야의 연이은 수상자 목록을 반환해요.
쿼리(Query)
CREATE OR REPLACE VIEW nobel_prize_laureates
AS SELECT *
FROM file('nobel_laureates_data.csv');
쿼리(Query)
SELECT
fullName,
lead(year, 1, year) OVER (PARTITION BY category ORDER BY year ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS year,
category,
motivation
FROM nobel_prize_laureates
WHERE category = 'physics'
ORDER BY year DESC
LIMIT 9
쿼리(Query)
┌─fullName─────────┬─year─┬─category─┬─motivation─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
1. │ Anne L Huillier │ 2023 │ physics │ for experimental methods that generate attosecond pulses of light for the study of electron dynamics in matter │
2. │ Pierre Agostini │ 2023 │ physics │ for experimental methods that generate attosecond pulses of light for the study of electron dynamics in matter │
3. │ Ferenc Krausz │ 2023 │ physics │ for experimental methods that generate attosecond pulses of light for the study of electron dynamics in matter │
4. │ Alain Aspect │ 2022 │ physics │ for experiments with entangled photons establishing the violation of Bell inequalities and pioneering quantum information science │
5. │ Anton Zeilinger │ 2022 │ physics │ for experiments with entangled photons establishing the violation of Bell inequalities and pioneering quantum information science │
6. │ John Clauser │ 2022 │ physics │ for experiments with entangled photons establishing the violation of Bell inequalities and pioneering quantum information science │
7. │ Giorgio Parisi │ 2021 │ physics │ for the discovery of the interplay of disorder and fluctuations in physical systems from atomic to planetary scales │
8. │ Klaus Hasselmann │ 2021 │ physics │ for the physical modelling of Earths climate quantifying variability and reliably predicting global warming │
9. │ Syukuro Manabe │ 2021 │ physics │ for the physical modelling of Earths climate quantifying variability and reliably predicting global warming │
└──────────────────┴──────┴──────────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘