student_id | weight (kg) |
---|---|
A100 | 50 |
A101 | 55 |
A124 | 55 |
B343 | 60 |
B346 | 72 |
C563 | 72 |
C345 | 72 |
윈도우 함수 사용
SELECT student_id,
ROW_NUMBER() OVER (ORDER BY student_id) AS seq
FROM Weights;
상관 서브쿼리 사용
SELECT student_id,
(SELECT COUNT(*)
FROM Weights W2
WHERE w2.student_id <= W1.student_id) AS seq
FROM Weights W1;
결과
student_id | seq |
---|---|
A100 | 1 |
A101 | 2 |
A124 | 3 |
B343 | 4 |
B346 | 5 |
C345 | 6 |
C563 | 7 |
class | student_id | weight |
---|---|---|
1 | 100 | 50 |
1 | 101 | 55 |
1 | 102 | 56 |
2 | 100 | 60 |
2 | 101 | 72 |
2 | 102 | 73 |
2 | 103 | 73 |
윈도우 함수 사용
SELECT class, student_id,
ROW_NUMBER() OVER (ORDER BY class, student_id) AS seq
FROM Weights2;
상관 서브쿼리 사용
SELECT class, student_id,
(SELECT COUNT(*)
FROM Weights2 W2
WHERE (W2.class, W2.student_id) <= (W1.class, W1.student_id) ) AS seq
FROM Weights2 W1;
다중 필드 비교