我不确定这是否可能,但我一直在尝试计算两个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
由于我并没有真正在互联网上找到类似的查询,因此我在这一方面有点迷失。谁能指出我正确的方向?
答案 0 :(得分:0)
尝试这种方法
select total + completed sumOfSums1
from (
sql from question goes here
) derivedTable