22,209
社区成员
发帖
与我相关
我的任务
分享
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'tba')
BEGIN
DROP TABLE tba
END
GO
CREATE TABLE tba
(
Age INT
)
GO
INSERT INTO tba
SELECT 30 UNION
SELECT 31 UNION
SELECT 43 UNION
SELECT 56 UNION
SELECT 12 UNION
SELECT 67 UNION
SELECT 25 UNION
SELECT 47 UNION
SELECT 1 UNION
SELECT 23 UNION
SELECT 78 UNION
SELECT 29
GO
SELECT RTRIM(Age / 10 * 10) + '岁-' + RTRIM((Age / 10 + 1) * 10) + '岁' AS Age,COUNT(1) AS Num
FROM tba
GROUP BY Age / 10
Age Num
0岁-10岁 1
10岁-20岁 1
20岁-30岁 3
30岁-40岁 2
40岁-50岁 2
50岁-60岁 1
60岁-70岁 1
70岁-80岁 1
select
sum(case when [age] between 1 and 10 then 1 else 0 end) as [0-10],
sum(case when [age] between 11 and 20 then 1 else 0 end) as [11-20],
sum(case when [age] between 21 and 30 then 1 else 0 end) as [21-30],
sum(case when [age] between 31 and 40 then 1 else 0 end) as [31-40],
sum(case when [age] between 41 and 50 then 1 else 0 end) as [41-50],
sum(case when [age] between 51 and 60 then 1 else 0 end) as [51-60],
sum(case when [age] between 61 and 70 then 1 else 0 end) as [61-70]
from tb