简体   繁体   中英

is it possible to calculate from SUM Columns?

i'm not sure if this is even possible but I've been trying to calculate the result of two SUM(CASE... results to be able to calculate the Percentage of completion for each refresh cycle.

its a bit complicated...

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;

which gets me this result


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

i'm getting a bit lost in this one since i'm not really finding similar query's on the internet. can anyone point me in the right direction?

Try this approach

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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