简而言之:在 MSSQL 2005 中使用 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 的中值,但这似乎效率低下且丑陋。
任何想法都感激不尽!