我有一个age
和name
customers
的SQL表格,想要计算以下年龄段内的客户数量
18-30
31-45
46-60
61 and above
任何想法我该怎么做?
答案 0 :(得分:0)
您可以在数据库中执行此操作
WITH C AS(
SELECT name
,age
,CASE
WHEN age >= 18 AND age <= 30 THEN '18-30'
WHEN age >= 31 AND age <= 45 THEN '31-45'
WHEN age >= 46 AND age <= 60 THEN '46-60'
WHEN age >= 61 THEN '>61'
ELSE NULL
END AS AgeGroup
FROM table
)
SELECT COUNT(AgeGroup) cnt, AgeGroup FROM C GROUP BY AgeGroup