1

我使用以下代码按年计算案例:

COUNT(CASE WHEN Year(FilingDate)=2008 THEN 1 ELSE NULL END) AS '2008'

我只想在另一列不同时计算这些情况。另一列称为“归档日期”,我想它看起来像这样:

COUNT(CASE distinct (DocketNumber) WHEN Year(FilingDate)=2008 THEN 1 ELSE NULL END) AS '2008',

样本记录:

案卷编号归档日期

123 2008

123 2008

123 2008

111 2009

112 2009

我只想收到 = 1 有什么想法吗?

谢谢

4

2 回答 2

1

将选项与SUBQUERYGROUP BY子句一起使用

SELECT COUNT(CASE WHEN YearFilingDate = 2008 THEN 1 END) AS '2008'
FROM
 (      
  SELECT Year(FilingDate) AS YearFilingDate, DocketNumber
  FROM dbo.test55
  GROUP BY Year(FilingDate), DocketNumber
  ) x

SQLFiddle上的演示

于 2013-03-26T08:14:22.060 回答
0
SELECT COUNT(CASE WHEN Year(FilingDate)=2008 THEN 1 ELSE NULL END) AS '2008'
GROUP BY DocketNumber

或许。

于 2013-03-26T06:29:00.430 回答