PARTITION & ORDER
Coddy SQL 여정의 기초 섹션에 포함된 레슨 — 72개 중 60번째.
또한 PARTITION BY와 ORDER BY를 함께 사용할 수 있습니다. 예를 들어 다음 table1을 고려해 보세요:
| id | type |
| 132 | t1 |
| 52 | t2 |
| 92 | t1 |
| 154 | t3 |
| 198 | t1 |
SELECT id, type, ROW_NUMBER() OVER (PARTITION BY type ORDER BY id) as row_num
FROM table1이것은 각 타입의 id에 따라 오름차순으로 행을 번호 매깁니다:
| id | type | row_num |
| 132 | t1 | 2 |
| 52 | t2 | 1 |
| 92 | t1 | 1 |
| 154 | t3 | 1 |
| 198 | t1 | 3 |
이제 id 132는 row_num 2를 가지게 됩니다. 왜냐하면 92보다 크고 198보다 작기 때문입니다 (모든 t1 타입 행 중에서).
챌린지
쉬움사용 가능한 테이블 및 컬럼:
<strong>doors</strong>:<strong>id</strong>,<strong>publication_year</strong><strong>doors_specs</strong>:<strong>id</strong>,<strong>country</strong>,<strong>color</strong>
각 국가 내 도어 색상의 순서를 분석하기 위해, 출판 연도 기준으로 정렬된 SQL 쿼리를 작성하세요. 각 도어 스페이스에 대해 다음을 표시하세요:
- 국가
- 색상
- 출판 연도
- 출판 연도를 기준으로 각 국가 내 색상의 순서를 나타내는 숫자. 이 컬럼을
color_sequence_number라고 호출하세요.
치트 시트
PARTITION BY를 ORDER BY와 함께 사용하여 각 파티션 내의 행을 특정 순서로 번호 매길 수 있습니다:
SELECT id, type, ROW_NUMBER() OVER (PARTITION BY type ORDER BY id) as row_num
FROM table1이것은 각 type 그룹 내에서 id를 오름차순으로 정렬하여 행을 번호 매깁니다. 각 파티션은 번호 매김을 1부터 다시 시작합니다.
직접 해보기
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
기초의 모든 레슨
4More Keywords
The IN keywordThe BETWEEN keywordThe LIKE keywordThe AS keywordRecap - Cellphone Models2Conditions
Conditions BasicsThe AND keywordThe OR keywordThe NOT keywordMultiple Conditions CombinedParenthesisBooleans5Arithmetic Operations
Mathematical OperatorsMathematical ColumnsThe Modulo OperationThe ROUND() Function8Statistics
Built-In Aggregate Part 1Built-In Aggregate Part 2Grouping Part 1Grouping Part 2Subqueries Part 1Subqueries Part 2Recap - Total Gain ShopRecap - Scooter ShopRecap - Coffee Shop11Window Functions part 1
ROW_NUMBER functionORDER BY criterionPARTITION BY criterionPARTITION & ORDERLEAD & LAG FunctionsRecap - LEAD & LAGRecap - PicturesRecap - Boxes3Specific Return Format
Null valuesSort Results Part 1Sort Results Part 2Recap - Cyber Security FirmLimit number of recordsRecap - Vehicle Factory6Intro Challenges
Recap - Parliamentary ElectionRecap - Police Criminal ArrestRecap - Bar Beverage ContainerRecap - Engineer new columns