2012-08-31 164 views
0

我試圖運行下面的查詢,但對於在我的Case語句中有兩個要求都被拔出的人,當我只想要單個記錄被拔​​出時,即使它們被拔出有兩個條件。Select Statement中的Case語句

SELECT DISTINCT 
    SyCampus.Descrip AS 'Campus', 
    dbo.rpt_adAttendanceDetail_vw.instructorname AS 'Instructor Name', 
    dbo.rpt_adAttendanceDetail_vw.classcode AS 'Class Code', 
    dbo.rpt_adAttendanceDetail_vw.section AS 'Section', 
    dbo.rpt_adAttendanceDetail_vw.classdescrip AS 'Class', 
    RTRIM(SyStudent.FirstName) + ' ' + ' ' + RTRIM(SyStudent.LastName) AS 'Student Name', 
    dbo.rpt_adAttendanceDetail_vw.stunum AS 'Student Number', 
    CASE WHEN CmEvent.CmtemplateID IN (714, 716, 732,734)THEN 'YES' ELSE 'NO' END AS 'Instructor Contact' 


FROM 
    dbo.rpt_adAttendanceDetail_vw 
JOIN 
    SyStudent 
     ON SyStudent.SyStudentID = dbo.rpt_adAttendanceDetail_vw.SyStudentID 
JOIN 
    SyCampus 
     ON Sycampus.SycampusID = SyStudent.SyCampusID 
JOIN 
    CmEvent 
     ON CmEvent.SyStudentID = SyStudent.SyStudentID 

WHERE dbo.rpt_adAttendanceDetail_vw.AttMin = '0' 
    AND dbo.rpt_adAttendanceDetail_vw.date = DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0) -1 
    AND SyStudent.SySchoolStatusID IN (13, 129, 130, 132, 72, 59, 122, 14) 
    AND dbo.rpt_adAttendanceDetail_vw.attendtype <> 'E' 
    AND CmEvent.CmEventStatusid = '2' 
+0

什麼是方言?微軟的SQL Server?其他? – ssarabando

+0

'CASE'永遠不會重複你的行 - 它會返回yes或no。 – dezso

回答

4

我假設你在SyStudent和CmEvent之間有一對多的關係。考慮到每個SyStudent可能有一個相應的CmEvent(714,716,732,734),它們可以解釋爲什麼你的查詢可能會爲每個SyStudent返回多於一條記錄。如果您想知道SyStudent在給定列表中是否有CmEvent.CmtemplateID,您可以在連接中處理它。

考慮以下更改查詢:


SELECT DISTINCT 
    SyCampus.Descrip AS 'Campus', 
    dbo.rpt_adAttendanceDetail_vw.instructorname AS 'Instructor Name', 
    dbo.rpt_adAttendanceDetail_vw.classcode AS 'Class Code', 
    dbo.rpt_adAttendanceDetail_vw.section AS 'Section', 
    dbo.rpt_adAttendanceDetail_vw.classdescrip AS 'Class', 
    RTRIM(SyStudent.FirstName) + ' ' + ' ' + RTRIM(SyStudent.LastName) AS 'Student Name', 
    dbo.rpt_adAttendanceDetail_vw.stunum AS 'Student Number', 
    CASE WHEN CmEvent.SyStudentID THEN 'YES' ELSE 'NO' END AS 'Instructor Contact' 
    FROM 
    dbo.rpt_adAttendanceDetail_vw 
JOIN 
    SyStudent 
     ON SyStudent.SyStudentID = dbo.rpt_adAttendanceDetail_vw.SyStudentID 
JOIN 
    SyCampus 
     ON Sycampus.SycampusID = SyStudent.SyCampusID 
LEFT JOIN 
    CmEvent 
     ON CmEvent.SyStudentID = SyStudent.SyStudentID 
     AND CmEvent.CmEventStatusid = '2' 
     AND CmEvent.CmtemplateID IN (714, 716, 732,734) 
WHERE dbo.rpt_adAttendanceDetail_vw.AttMin = '0' 
    AND dbo.rpt_adAttendanceDetail_vw.date = DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0) -1 
    AND SyStudent.SySchoolStatusID IN (13, 129, 130, 132, 72, 59, 122, 14) 
    AND dbo.rpt_adAttendanceDetail_vw.attendtype <> 'E' 

兩個顯著這裏的變化是左連接和case語句。首先,我們已經將CmEvent條件移入左連接。通過這樣做,我們只會加入CmEvent中符合我們標準的記錄。這將省去模板ID超出我們列表的所有CmEvent記錄。其次,案例陳述發生了變化。現在我們使用CmEvent.SyStudentID的存在來確定SyStudent在給定列表中是否具有CmEvent.CmtemplateID。如果左連接不產生匹配,則知道它們不匹配。

+0

+1第一次回答不錯:)我正在寫類似的東西。 – podiluska

1

我的猜測是你想要一個外部聚合查詢返回max('教師聯繫')。

而且,考慮使用1而不是「是」和0,而不是「不」

也可以考慮使用表的別名。

1

case被有效地取代這些號碼714,716之一,732,734用是或否,因此樣子重疊,但它在做什麼你問它是什麼。我想如果你GROUP BY所有的SELECT字段你應該有不同的結果。

您也可以在這裏看看上SQL FIDDLE

所以,你可以這樣做:

SELECT 
     SyCampus.Descrip AS 'Campus', 
     dbo.rpt_adAttendanceDetail_vw.instructorname AS 'Instructor Name', 
     dbo.rpt_adAttendanceDetail_vw.classcode AS 'Class Code', 
     dbo.rpt_adAttendanceDetail_vw.section AS 'Section', 
     dbo.rpt_adAttendanceDetail_vw.classdescrip AS 'Class', 
     RTRIM(SyStudent.FirstName) + ' ' + ' ' + RTRIM(SyStudent.LastName) AS 'Student Name', 
     dbo.rpt_adAttendanceDetail_vw.stunum AS 'Student Number', 
     CASE WHEN CmEvent.CmtemplateID IN (714, 716, 732,734)THEN 'YES' ELSE 'NO' END AS 'Instructor Contact' 


    FROM 
     dbo.rpt_adAttendanceDetail_vw 
    JOIN 
     SyStudent 
      ON SyStudent.SyStudentID = dbo.rpt_adAttendanceDetail_vw.SyStudentID 
    JOIN 
     SyCampus 
      ON Sycampus.SycampusID = SyStudent.SyCampusID 
    JOIN 
     CmEvent 
      ON CmEvent.SyStudentID = SyStudent.SyStudentID 

    WHERE dbo.rpt_adAttendanceDetail_vw.AttMin = '0' 
     AND dbo.rpt_adAttendanceDetail_vw.date = DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0) -1 
     AND SyStudent.SySchoolStatusID IN (13, 129, 130, 132, 72, 59, 122, 14) 
     AND dbo.rpt_adAttendanceDetail_vw.attendtype <> 'E' 
     AND CmEvent.CmEventStatusid = '2' 
    GROUP BY 
     SyCampus.Descrip, 
     dbo.rpt_adAttendanceDetail_vw.instructorname, 
     dbo.rpt_adAttendanceDetail_vw.classcode, 
     dbo.rpt_adAttendanceDetail_vw.section, 
     dbo.rpt_adAttendanceDetail_vw.classdescrip, 
     RTRIM(SyStudent.FirstName) + ' ' + ' ' + RTRIM(SyStudent.LastName), 
     dbo.rpt_adAttendanceDetail_vw.stunum, 
     CASE WHEN CmEvent.CmtemplateID IN (714, 716, 732,734)THEN 'YES' ELSE 'NO' END 

在一個小的查詢有可能沒有太多的收穫,但我認爲GROUP BY最好DISTINCT在性能方面:reference here