簡體   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