2013-05-02 77 views
-5

我要帶兩個下面的語句,只得到了結果於2013年限制SQL語句年

SELECT 
    COUNT(DISTINCT 
    CAST(c1.caseseqnumber AS VARCHAR) + '-' + CAST(c1.year AS VARCHAR)), 
    COUNT(DISTINCT 
    CAST(c2.caseseqnumber AS VARCHAR) + '-' + CAST(c2.year AS VARCHAR)) 
FROM 
    caseparticipants c1 LEFT JOIN caseparticipants c2 
    ON c1.year=c2.year AND c1.caseseqnumber=c2.caseseqnumber 
    AND c2.participanttype = 'Appellant Rep 1' 

和第二個

SELECT DISTINCT 
    c1.caseseqnumber, 
    c1.year 
FROM 
    caseparticipants c1 LEFT JOIN caseparticipants c2 
    ON c1.year=c2.year AND c1.caseseqnumber=c2.caseseqnumber 
    AND c2.participanttype = 'Appellant Rep 1' 
WHERE 
    c2.year IS NULL 
Order by year ASC, Caseseqnumber ASC 

謝謝!

+0

你嘗試過什麼?我看到「WHERE c2.year IS NULL」,所以這不會那麼困難。 – 2013-05-02 14:21:44

+0

當我嘗試它刪除所有正確的結果。如果我做c2.year ='2013'我沒有得到我需要的結果集 – parkjohnston 2013-05-02 14:22:33

+0

沒錯,但我們不知道你得到了什麼結果集,以及你在找什麼。不確定任何人都可能提供幫助。 – 2013-05-02 14:23:37

回答

2

如果您似乎已經有一個year場,所以只需添加到您的WHERE條款:

1:

SELECT COUNT(DISTINCT CAST(c1.caseseqnumber AS VARCHAR) + '-' + CAST(c1.year AS VARCHAR)), COUNT(DISTINCT CAST(c2.caseseqnumber AS VARCHAR) + '-' + CAST(c2.year AS VARCHAR)) 
FROM caseparticipants c1 
LEFT JOIN caseparticipants c2 
    ON c1.year=c2.year AND c1.caseseqnumber=c2.caseseqnumber 
    AND c2.participanttype = 'Appellant Rep 1' 
WHERE c1.year = 2013 

2:

SELECT DISTINCT c1.caseseqnumber, c1.year 
FROM caseparticipants c1 
LEFT JOIN caseparticipants c2 
    ON c1.year=c2.year AND c1.caseseqnumber=c2.caseseqnumber 
    AND c2.participanttype = 'Appellant Rep 1' 
WHERE c1.year = 2013 
    AND c2.year IS NULL 
ORDER BY year ASC, Caseseqnumber ASC 
+0

空是讓我不在表中的結果..這是我得到正確的結果。 – parkjohnston 2013-05-02 14:23:36

+0

那麼你需要記錄哪裏'c1.year'是2013年,但是'c2'沒有匹配? – 2013-05-02 14:25:27

+0

正確...只是不知道該怎麼做 – parkjohnston 2013-05-02 14:26:32