1
簡而言之:在MSSQL 2005中使用WITH CUBE時,是否有任何方法直接計算中間值,最好不使用循環?如何在使用WITH CUBE時獲得中位數值?
一些代碼:
CREATE TABLE #Example (
ID1 INT,
ID2 INT,
[Value] INT
)
INSERT #Example(ID1,ID2,[Value])
SELECT 1, 1, 45 UNION
SELECT 1, 2, 78 UNION
SELECT 1, 3, 23 UNION
SELECT 1, 4, 119 UNION
SELECT 1, 5, 85 UNION
SELECT 2, 1, 143 UNION
SELECT 2, 2, 124 UNION
SELECT 2, 3, 161 UNION
SELECT 2, 4, 171 UNION
SELECT 2, 5, 142
SELECT ID1, ID2, COUNT(1) AS 'NumValues', MIN([Value]) AS 'Minimum', AVG([Value]) AS 'Average', MAX([Value]) AS 'Maximum'
FROM #Example
GROUP BY ID1, ID2
WITH CUBE
HAVING ID1 IS NULL OR ID2 IS NULL
ORDER BY ISNULL(ID1, 0), ISNULL(ID2, 0)
DROP TABLE #Example
我如何獲得平均值的列?
迄今爲止管理得最好的是循環ID1和ID2值,並計算每組ID的中位數,但這似乎是低效和醜陋的。
任何想法感激地收到!