繁体   English   中英

可以从SUM列进行计算吗?

[英]is it possible to calculate from SUM Columns?

我不确定这是否可能,但我一直在尝试计算两个SUM(CASE ...)的结果,以便能够计算每个刷新周期的完成百分比。

这有点复杂...

SELECT RefreshCycle,

SUM(CASE WHEN RefreshStatus IN ('In Progress', 'Not Started', 'Not Due') and Status = 'Deployed' THEN 1 ELSE 0 END) + SUM(CASE WHEN RefreshStatus = 'Completed' and Status NOT IN ('Deployed') THEN 1 ELSE 0 END)  as 'Total', 
SUM(CASE WHEN RefreshStatus = 'Completed' and Status NOT IN ('Deployed') THEN 1 ELSE 0 END) as 'Completed', 
SUM(CASE WHEN RefreshStatus = 'In Progress' and Status = 'Deployed' THEN 1 ELSE 0 END ) as 'In Progress', 
SUM(CASE WHEN RefreshStatus = 'Not Started' and Status = 'Deployed' THEN 1 ELSE 0 END) as 'Not Started', 
SUM(CASE WHEN RefreshStatus = 'Not Due' and Status = 'Deployed' THEN 1 ELSE 0 END)  as 'Not Due', 
SUM(CASE WHEN RefreshStatus = 'Not Eligible' and Status = 'Deployed' THEN 1 ELSE 0 END)  as 'Not Eligible'

--('Total') / ('Completed') *100.00 as 'Percentage'

--SUM(CASE WHEN RefreshStatus IN ('In Progress', 'Not Started', 'Not Due') and Status = 'Deployed' THEN 1 ELSE 0 END) + SUM(CASE WHEN RefreshStatus = 'Completed' and Status NOT IN ('Deployed') THEN 1 ELSE 0 END) / SUM(CASE WHEN RefreshStatus = 'Completed' and Status NOT IN ('Deployed') THEN 1 END)* 100.00 as 'Completed' 

FROM Hardware_Inventory 

WHERE Status NOT IN ('In Stock', 'Parts') 
and Manufacturer NOT IN ('Apple') 
and AssetType NOT IN ('Monitor','Docking Station','Optical Drive','Other', 'Projector', 'Scanner/Printer', 'BlackBerry', 'Server') 
GROUP BY RefreshCycle
ORDER BY RefreshCycle asc;

这给我这个结果


RefreshCycle    Total   Completed   In Progress Not Started Not Due Not Eligible
2008/09           38          38           0           0       0    3
2009/10          236         236           0           0       0    8
2010/11          263         263           0           0       0    4
2011/12          192         192           0           0       0    3
2012/13         1350        1349           0           1       0    40
2013/14         1828        1815           0          13       0    63
2014/15         1219        1160           0          59       0    314
2015/16         1866        1658           0         208       0    355
2016/17          696         397           0         299       0    189
2017/18         2782           9           0           0    2773    198
2018/19         1472           5           0           1    1466    185
2019/20         1107           0           0           0    1107    41
2020/21         2160           0           0           2    2158    125
2021/22          421           0           0           4     417    32

由于我在互联网上找不到真正的类似查询,因此我对此一无所知。 谁能指出我正确的方向?

试试这个方法

select total + completed sumOfSums1
from (
sql from question goes here
) derivedTable

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM