-5

我想采用以下两个语句并仅获得 2013 年的结果。

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

谢谢!

4

1 回答 1

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
于 2013-05-02T14:22:27.353 回答