2014-05-23 85 views
0

我需要在三個不同的組(30%,30%和40%)中劃分查詢結果。如果我使用NTILE(3)函數,結果被分成三個相等的組(33%,33%,33%)。 我該怎麼辦?TSQL:除以百分比結果

這是我的查詢的模式:

;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 

非常感謝您

+0

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

回答

1

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