2017-01-30 8 views
0

これも可能かどうかはわかりませんが、私は2回のSUM(CASE ...結果)を計算して、リフレッシュサイクルごとに完了率を計算できるようにしています。SUM列から計算することは可能ですか?

私は実際に見つけていないよので、私はこの1つで失われたビットを取得しています


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 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; 

インターネット上の同様のクエリです。誰も私を正しい方向に向けることができますか?

答えて

0

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

現在サーバー2008このアプローチを試してみて文句を言わない私は新しい列に完成し、総使用して完了%を計算するために、新たな合計で再利用するために、私は合計で作成した列を使用してみましょう。 – GhislainJC

+0

あなたが実際に私が示唆しているものを試してみることになります。 –

関連する問題