日期:2014-05-17 浏览次数:20409 次
CREATE TABLE #TEMP (Id INT,TYPE INT,NAME VARCHAR(10))
INSERT INTO #TEMP
SELECT 1,1,'jack' UNION ALL
SELECT 2,1,'tony' UNION ALL
SELECT 3,2,'sum' UNION ALL
SELECT 4,3,'luss'
;WITH a1 AS
(
SELECT TYPE,COUNT(*) SL,ROW_NUMBER() OVER(ORDER BY TYPE) RE
FROM #temp
GROUP BY TYPE
)
SELECT CAST(CAST(100.*(SELECT SUM(sl) FROM a1 WHERE re<=a.re)/(SELECT SUM(sl) FROM a1) AS INT) AS VARCHAR(3))+'%'
FROM a1 a
ORDER BY TYPE
CREATE TABLE #tb (Id INT,TYPE INT,NAME VARCHAR(10))
INSERT INTO #tb
SELECT 1,1,'jack' UNION ALL
SELECT 2,1,'tony' UNION ALL
SELECT 3,2,'sum' UNION ALL
SELECT 4,3,'luss'
;with cte as
(
select TYPE,COUNT(1) as num from #tb group by type
)
select str(cast(100.0*(select SUM(num) from cte t where t.TYPE<=a.TYPE)/b.total as int))+'%' as result
from cte a,(select COUNT(*) as total from #tb)b