我有以下查询:
SELECT
ISNULL(DrugName,'Sub Total:') as Drug,
SUM(COUNT) as Count,
Percentage
FROM
(
SELECT
DrugName,
DrugCategoryName,
COUNT(*) as Count,
CONVERT(DECIMAL(10,2),COUNT(*) * 100.0 / SUM(COUNT(*)) over ()) as Percentage
FROM
Visit V
INNER JOIN Drug D on
V.DrugID = D.DrugID
INNER JOIN DrugCategory DC on
D.DrugCategoryID = DC.DrugCategoryID
GROUP BY GROUPING SETS
((DrugName, DrugCategoryName))
) a
GROUP BY GROUPING SETS
((DrugName, DrugCategoryName, Percentage), (DrugCategoryName))
给出了以下结果:
Drug Count Percentage
Amphetamines 401 4.24
Benzodiazapine 435 4.60
Biodone 459 4.85
Sub Total: 1295 NULL
Brown Heroin 436 4.61
Buprenorphine 396 4.18
Cocaine 444 4.69
Did not inject 404 4.27
Endone 450 4.75
Fentanyl Patch 404 4.27
Heroin 1365 14.42
Heroin & Cocaine 448 4.73
Ice/Crystal/Meth 889 9.39
Sub Total: 5236 NULL
Kapanol 427 4.51
Methadone 430 4.54
Methadone Syrup 394 4.16
Morphine 417 4.41
MS Contin 438 4.63
MS Mono 424 4.48
Other Amphetamines 404 4.27
Sub Total: 2934 NULL
我还需要做的是计算子总百分比,所以例如第一个子总数是1295,这样可以根据1295 + 5236 + 2934计算一个百分比,得到的值为13.68%,等等其他子总计行。
这怎么可以动态完成,所以无论Sub Totals的数量是多少,它都能正确计算出来?
答案 0 :(得分:1)
WITH CTE AS
(
SELECT
ISNULL(DrugName,'Sub Total:') as Drug,
SUM(COUNT) as Count,
Percentage
FROM
(
SELECT
DrugName,
DrugCategoryName,
COUNT(*) as Count,
CONVERT(DECIMAL(10,2),COUNT(*) * 100.0 / SUM(COUNT(*)) over ()) as Percentage
FROM
Visit V
INNER JOIN Drug D on
V.DrugID = D.DrugID
INNER JOIN DrugCategory DC on
D.DrugCategoryID = DC.DrugCategoryID
GROUP BY GROUPING SETS
((DrugName, DrugCategoryName))
) a
GROUP BY GROUPING SETS
((DrugName, DrugCategoryName, Percentage), (DrugCategoryName)))
SELECT [DRUG],[COUNT] ,ISNULL([PERCENTAGE],([COUNT]/S)*100) AS [PERCENTAGE] FROM CTE A CROSS APPLY
(SELECT CAST(SUM([COUNT])AS NUMERIC(22,6)) AS S FROM CTE B WHERE DRUG='SUB TOTAL:')C
输出
DRUG COUNT PERCENTAGE
Amphetamines 401 4.240000
Benzodiazapine 435 4.600000
Biodone 459 4.850000
Sub Total: 1295 13.681986
Brown Heroin 436 4.610000
Buprenorphine 396 4.180000
Cocaine 444 4.690000
Did not inject 404 4.270000
Endone 450 4.750000
Fentanyl Patch 404 4.270000
Heroin 1365 14.420000
Heroin & Cocaine 448 4.730000
Ice/Crystal/Meth 889 9.390000
Sub Total: 5236 55.319599
Kapanol 427 4.510000
Methadone 430 4.540000
Methadone Syrup 394 4.160000
Morphine 417 4.410000
MS Contin 438 4.630000
MS Mono 424 4.480000
Other Amphetamines 404 4.270000
Sub Total: 2934 30.998415
答案 1 :(得分:0)
我认为你可以只计算子查询中的总数并除以:
SELECT COALESCE(DrugName, 'Sub Total:') as Drug,
SUM(COUNT) as Count,
SUM(COUNT) / total_cnt as Percentage
FROM (SELECT DrugName, DrugCategoryName,
COUNT(*) as Count,
CONVERT(DECIMAL(10,2), COUNT(*)) * 100.0 / SUM(COUNT(*)) over () as Percentage,
SUM(1.0*COUNT(*)) OVER () as total_cnt
FROM Visit V INNER JOIN
Drug D
ON V.DrugID = D.DrugID INNER JOIN
DrugCategory DC
ON D.DrugCategoryID = DC.DrugCategoryID
GROUP BY DrugName, DrugCategoryName
) vd
GROUP BY GROUPING SETS ((DrugName, DrugCategoryName, Percentage, Total_cnt),
(DrugCategoryName, Total_Cnt))