2014-02-18 161 views
0

我有一張表格,裏面有學生的重複註冊表,但每行代表該學生的課程和狀態。在同一個表格中比較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 
+2

預計產量? – OGHaza

+0

我更新了表格。 在Daniel Cho的情況下,我需要帶有CURSANDO STATUS的行。 –

+0

您是否只在尋找某個課程中的狀態發生變化的學生,或者您是否正在尋找學生每個課程的最新狀態? –

回答

1

這個查詢將找到最新行爲每個學生/課程組合。它使用Common Table Expression來查找每個STUDENT/COURSE組合的最新日期,然後使用該CTE來獲取匹配的行。最終結果是每個STUDENT/COURSE組合的最新行。

WITH 
CTE_MostRecent AS (
    -- For each student/course combination, retrieve: 
    -- * student ID 
    -- * course 
    -- * date of most recent entry 
    SELECT ID, 
     COURSE, 
     MAX(DATE) AS MaxDate -- Most recent date 
    FROM StudentCourses 
    GROUP BY ID, 
      COURSE 
) 
SELECT S.* 
FROM StudentCourses AS S 
-- Only select the the most recent row 
-- for this STUDENT/COURSE combination 
INNER JOIN CTE_MostRecent AS M 
    ON S.ID = M.ID 
    AND S.COURSE = M.COURSE 
    AND S.DATE = M.MaxDate 

輸出(SQLFiddle):

╔════════╦═════════════╦═════════════════════╦═══════════════╦═══════════╗ 
║ ID ║ STUDENT ║  DATE   ║ COURSE  ║ STATUS ║ 
╠════════╬═════════════╬═════════════════════╬═══════════════╬═══════════╣ 
║ 105937 ║ DANIEL CHO ║ 2014-02-14 00:00:00 ║ ADMINISTRAÇÃO ║ CURSANDO ║ 
║ 39961 ║ LUIZ FELIPE ║ 2014-02-12 00:00:00 ║ ADMINISTRAÇÃO ║ CURSANDO ║ 
║ 39316 ║ IGOR BASTOS ║ 2008-04-07 00:00:00 ║ CIÊNCIAS  ║ CANCELADO ║ 
║ 39316 ║ IGOR BASTOS ║ 2014-01-08 00:00:00 ║ ADMINISTRAÇÃO ║ FORMADO ║ 
║ 21245 ║ ROBERTA ZOR ║ 2014-01-08 00:00:00 ║ CIÊNCIAS  ║ FORMAD ║ 
╚════════╩═════════════╩═════════════════════╩═══════════════╩═══════════╝ 

注:輸出上述是從實際的SQL-Server實例截取,而不是從SQLFiddle。 SQLFiddle顯示DATETIME值 「[MONTHNAME],DD YYYY 14 HH:MM:SS + 0000」

注:此解決方案假定您已經在每人每天STUDENT/COURSE組合最爲一個條目。

+0

我需要從組合STUDENT /課程的最新狀態爲所有學生。 –

+0

我更新了我的答案,以說明如何做到這一點。 –

0

查詢是

SELECT a.id, a.student, a.course, MAX(a.date) as hight_date 
FROM table a 
INNER JOIN table b on a.course = b.course 
WHERE a.status != b.status 
GROUP BY a.id,a.student, a.course 
+0

這有效,但它不會告訴你'STATUS'是什麼。 –

+0

如果您想查看狀態,請在select和group中添加列 – Ganz

+0

您的SQL存在缺陷。 'INNER JOIN'只確保每個'COURSE'至少有兩個不同的'STATUS'值,不管他們被分配了什麼學生。使用當前數據,通過刪除它可以得到相同的結果。如果同一'COURSE'中的所有學生只有一個「STATUS」值(並且它是相同的),那麼您將不會獲得行。 –

0
select * from 
    (select *,ROW_NUMBER()over(partition by COURSE,STATUS order by dates)rn 
    from @student)t4 where rn=1 
相關問題