我有一張表格,裏面有學生的重複註冊表,但每行代表該學生的課程和狀態。在同一個表格中比較2行之間的差異
我使用的是SQL SERVER 2008
類似的東西:
+--------+-------------+-------------------------+---------------+-----------------+
| ID | STUDENT | DATE | COURSE | STATUS |
+--------+-------------+-------------------------+---------------+-----------------+
| 21245 | ROBERTA ZOR | 2014-01-08 00:00:00.000 | CIÊNCIAS | FORMADO |
| 39316 | IGOR BASTOS | 2008-04-07 00:00:00.000 | CIÊNCIAS | CANCELADO |
| 39316 | IGOR BASTOS | 2014-01-08 00:00:00.000 | ADMINISTRAÇÃO | FORMADO |
| 39961 | LUIZ FELIPE | 2014-02-12 00:00:00.000 | ADMINISTRAÇÃO | CURSANDO |
| 105937 | DANIEL CHO | 2014-02-14 00:00:00.000 | ADMINISTRAÇÃO | CURSANDO |
| 105937 | DANIEL CHO | 2014-02-10 00:00:00.000 | ADMINISTRAÇÃO | RESERVA DE VAGA |
+--------+-------------+-------------------------+---------------+-----------------+
我需要從組合STUDENT /課程的最新狀態爲所有學生。
UPDATE
爲了讓我使用狀態的另一個連接:
SELECT a.ID, a.STUDENT, a.COURSE, MAX(a.DATE) as DATE
into #TABLE
FROM #STUDENTS a
INNER JOIN #STUDENTS b
on a.ID = a.ID
and a.COURSE = b.COURSE
and a.STATUS <> b.STATUS
GROUP BY a.ID,a.STUDENT, a.COURSE
select c.ID, c.STUDENT, c.COURSE, c.STATUS
into #FINAL_TABLE
from #TABLE t
inner join #STUDENTS C
on C.ID = T.ID and C.STUDENT = T.STUDENT and C.COURSE = T.COURSE
預計產量? – OGHaza
我更新了表格。 在Daniel Cho的情況下,我需要帶有CURSANDO STATUS的行。 –
您是否只在尋找某個課程中的狀態發生變化的學生,或者您是否正在尋找學生每個課程的最新狀態? –