34,576
社区成员
发帖
与我相关
我的任务
分享
DECLARE @pct FLOAT=0.2
;WITH a(id,age) AS (
select 1 ,10 UNION
select 2,10 UNION
select 3 ,20 UNION
select 4 ,20 UNION
select 5 ,20 UNION
select 6,30 UNION
select 7 ,30 UNION
select 8,30 UNION
select 9,30 UNION
select 10,30
),b AS (
SELECT *,NTILE(cast(1/@pct AS INT)) OVER(ORDER BY id) g FROM a
)
SELECT MAX(age) AS age,g*@pct*100 AS parcent FROM b GROUP BY g
最终结果,下面,你再把parcent转为字符加上百分号
age parcent
10 20
20 40
30 60
30 80
30 100