Oracle SQL查询 - >在2种不同条件下计数2列

时间:2013-02-12 21:50:28

标签: sql oracle count

我有一个看起来像这样的表:

YEAR        RESOLUTION_DATE      CREATION_DATE  

2013                             2013/02/18
2012                             2012/05/26
2009                             2009/11/11
2013          2013/12/08         2013/12/01
2000                             2000/17/31
2007                             2007/12/08
2012                             2012/12/08
2012          2012/03/23         2012/03/10 
2012                             2012/12/08
2007                             2007/01/17
2012          2012/01/17         2012/01/10
2009                             2009/02/14  

我正在尝试进行输出以下内容的查询:

YEAR      COUNT_RESOLUTION_DATE      COUNT_CREATION_DATE  
2000               0                          1
2007               0                          2
2009               0                          2
2011               0                          0
2012               2                          5
2013               1                          2  

需要注意的是,我希望查询按年计算RESOLUTION_DATE的数量,其中RESOLUTION_DATE为非NULL并且我想计算所有CREATION_DATE的数量。 oracle数据库需要SQL。

2 个答案:

答案 0 :(得分:4)

试试这个:

SELECT 
COUNT(RESOLUTION_DATE) AS COUNT_RESOLUTION_DATE, 
COUNT(CREATION_DATE) AS COUNT_CREATION_DATE
FROM MyTable
GROUP BY YEAR
ORDER BY YEAR

答案 1 :(得分:3)

如果您只想计算非NULL解析日期,则应该可以:

SELECT 
SUM(CASE WHEN RESOLUTION_DATE IS NULL THEN 0 ELSE 1 END) AS COUNT_RESOLUTION_DATE, 
COUNT(CREATION_DATE) AS COUNT_CREATION_DATE
FROM MyTable
GROUP BY YEAR
ORDER BY YEAR;