将SQL语句限制为年份

时间:2013-05-02 14:18:45

标签: sql

我想采取以下两个声明,只获得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

谢谢!

1 个答案:

答案 0 :(得分: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