如何对已分组的查询的结果进行分组?

时间:2015-12-10 19:13:24

标签: sql sql-server group-by

我有一个问题:

SELECT

CONVERT(varchar(7),SUBMITDATE, 120) as 'Month'

,CASE WHEN ReportType = '1' THEN (SELECT AVG(DATEDIFF(DAY,SUBMITDATE,DateClosed))) END as 'Report1Avg'
,CASE WHEN ReportType = '2' THEN (SELECT AVG(DATEDIFF(DAY,SUBMITDATE,DateClosed))) END as 'Report2Avg'
,CASE WHEN ReportType = '3' THEN (SELECT AVG(DATEDIFF(DAY,SUBMITDATE,DateClosed))) END as 'Report3Avg'
,CASE WHEN ReportType = '4' THEN (SELECT AVG(DATEDIFF(DAY,SUBMITDATE,DateClosed))) END as 'Report4Avg'
,CASE WHEN ReportType = '5' THEN (SELECT AVG(DATEDIFF(DAY,SUBMITDATE,DateClosed))) END as 'Report5Avg'

FROM Table1

    WHERE STATUS = 'Closed'

GROUP BY CONVERT(varchar(7),SUBMITDATE, 120), ReportType
ORDER BY CONVERT(varchar(7),SUBMITDATE, 120)

产生以下结果:

query results

我的问题是:如何将每个月的结果合并为一行? 恩。对于'2015-06',我有3行结果。

这可能吗?

1 个答案:

答案 0 :(得分:1)

import from StringIO import StringIO import urllib file_like_object = StringIO(urllib.urlopen(url).read()) 表达式周围使用avg。还可以使用case来避免空值。

else 0