TSQL:按百分比划分结果

时间:2014-05-23 15:19:06

标签: sql-server tsql

我需要在三个不同的组(30%,30%和40%)中对查询结果进行分区。 如果我使用NTILE(3)函数,结果将分为三个等于组(33%,33%,33%)。 我该怎么办?

这是我的查询架构:

;WITH TMP1 AS (
    SELECT
        Column0,
        ISNULL(SUM(Column1),0) AS Val
    FROM MyTable
    GROUP BY Column0
),
TMP2 AS (
SELECT NTILE (3) OVER (ORDER BY Column0 DESC) AS MyGroup, Val
FROM TMP1
)
SELECT MyGroup, ISNULL(SUM(Val),0) as Val
FROM TMP2
GROUP BY MyGroup
ORDER BY MyGroup

非常感谢

1 个答案:

答案 0 :(得分:1)

使用ntile(10)(1,2,3) 30%,(4,5,6) 30%,(7,8,9,10) 40%