我有一个问题:
SELECT CAST(SUBMITDATE as date) as 'Date'
,COUNT(*) as [Tickets Submitted]
,SUM(CASE WHEN STATUS = 'Closed' THEN 1 ELSE 0 END) as [Tickets Closed]
FROM Table1
WHERE SUBMITDATE >= '2015-05-29'
AND SUBMITDATE < '2015-06-03'
GROUP BY CAST(SUBMITDATE as date)
产生以下结果:
Date Tickets Submitted Tickets Closed
2015-05-29 1062 1040
2015-05-30 429 416
2015-05-31 260 250
2015-06-01 705 654
2015-06-02 556 515
2015-06-03 508 452
我想计算已关闭的门票百分比。 [门票关闭]除以[提交门票]。
当我尝试添加:,SUM((CASE WHEN STATUS = 'Closed' THEN 1 ELSE 0 END)/COUNT(*)) as [Percentage of Tickets Closed]
我收到以下错误:
&#34;无法对包含聚合或子查询的表达式执行聚合函数。&#34;
有人可以帮忙吗?
非常感谢。
答案 0 :(得分:1)
不要将COUNT放在SUM中。试试这样:
,CAST(SUM(CASE WHEN mrSTATUS = 'Closed' THEN 1 ELSE 0 END) AS float)/CAST(COUNT(*) AS float) as [Percentage of Tickets Closed]
SUM和COUNT函数将产生整数,这意味着除法也将产生整数。将它们作为花车进行铸造以获得分数。
答案 1 :(得分:0)
,CAST(CAST(SUM(CASE WHEN STATUS = 'Closed' THEN 1 ELSE 0 END) as FLOAT)/CAST(COUNT(*) as FLOAT)*100 as DECIMAL(5,2)) as [% of Tickets Closed]
是我正在寻找的。 p>
以下是我得到的结果:
Date Tickets Submitted Tickets Closed % of Tickets Closed
2015-05-29 1062 1047 98.59
2015-05-30 428 418 97.66
2015-05-31 260 253 97.31
2015-06-01 703 669 95.16
2015-06-02 555 517 93.15
2015-06-03 507 463 91.32