SQL 按年龄范围分组

问题描述 投票:0回答:7

SQL 2005, 我有一个包含“ages_c”列的表,我需要按年龄范围对记录进行分组。 这是我在这个网站上找到的查询,它让我达到了 90%,但“分组依据”出错,*无效的列名“age_range”*

 select 
  case
   when age_c <18 then 'Under 18'
   when age_c between 18 and 24 then '18-24'
   when age_c between 25 and 34then '25-34'
 END as age_range, 
 Count(*) as count
 from contacts
 group by age_range
 order by age_range

当我按“age_c”分组和排序时,我的结果是:

  Under 18  1
  18-24 1
  18-24 1
  25-34 1

我想要的是:

 Under 18   1
  18-24 2      
  25-34 1

谢谢。

sql sql-server sql-server-2005 group-by
7个回答
32
投票

试试这个方法:

 SELECT SUM(CASE WHEN age_c < 18 THEN 1 ELSE 0 END) AS [Under 18],
        SUM(CASE WHEN age_c BETWEEN 18 AND 24 THEN 1 ELSE 0 END) AS [18-24],
        SUM(CASE WHEN age_c BETWEEN 25 AND 34 THEN 1 ELSE 0 END) AS [25-34]
 FROM contacts

11
投票

Group by

age_c
——age_range 不是物理列。更具体地说,请执行以下操作:

group by case
   when age_c <18 then 'Under 18'
   when age_c between 18 and 24 then '18-24'
   when age_c between 25 and 34then '25-34'
 END

由于

age_range
是别名列,因此
group by
根本不知道它。分组发生在计算列集之前。您可以在其中使用别名的唯一子句是
order by
,因为这是在计算列集之后执行的唯一子句。


3
投票
您无法按在查询中创建的列进行分组。你必须这样做:

SELECT count(*), * FROM ( select case when age_c <18 then 'Under 18' when age_c between 18 and 24 then '18-24' when age_c between 25 and 34then '25-34' END as age_range from contacts ) t group by age_range order by age_range

或分组依据

case when age_c <18 then 'Under 18' when age_c between 18 and 24 then '18-24' when age_c between 25 and 34then '25-34' END
    

1
投票
如果您的数据库支持

FILTER WHERE 语法,那么可以以非常优雅的方式存档:

SELECT COUNT(id) FILTER (WHERE (age < 18)) AS "Under 18", COUNT(id) FILTER (WHERE (age >= 18 AND age <= 24)) AS "18-24", COUNT(id) FILTER (WHERE (age >= 25 AND age <= 34)) AS "25-34" FROM contacts

或者这个,如果没有:

SELECT count(CASE WHEN (age < 18) THEN id ELSE null END) AS "Under_18", count(CASE WHEN (age >= 18 AND age <= 24) THEN id ELSE null END) AS "18-24", count(CASE WHEN (age >= 25 AND age <= 34) THEN id ELSE null END) AS "25-34" FROM contacts
    

0
投票
这是您正在使用的实际代码吗?它看起来不像,因为你在 34 和 then 之间缺少一个空格。该代码在 SQL 中会出错。介意分享实际的未修改的查询吗?

无论如何,您可以使用临时表或嵌套查询。

SELECT CASE WHEN age_c <18 THEN 'Under 18' WHEN age_c BETWEEN 18 AND 24 THEN '18-24' WHEN age_c BETWEEN 25 AND 34 THEN '25-34' END AS age_range, INTO #TempAges FROM contacts ORDER BY age_c SELECT COUNT(*) FROM #TempAges GROUP BY age_range

使用完毕后不要忘记删除临时表


0
投票
CASE WHEN DATEDIFF(YEAR, DateOfBirth, GETDATE()) BETWEEN 20 AND 29 THEN '20-29' WHEN DATEDIFF(YEAR, DateOfBirth, GETDATE()) BETWEEN 30 AND 39 THEN '30-39' WHEN DATEDIFF(YEAR, DateOfBirth, GETDATE()) BETWEEN 40 AND 49 THEN '40-49' WHEN DATEDIFF(YEAR, DateOfBirth, GETDATE()) BETWEEN 50 AND 59 THEN '50-59' ELSE '60 AND Older' END AS age_group_Frequency, count (CASE WHEN DATEDIFF(YEAR, DateOfBirth, GETDATE()) BETWEEN 20 AND 29 THEN '20-29' WHEN DATEDIFF(YEAR, DateOfBirth, GETDATE()) BETWEEN 30 AND 39 THEN '30-39' WHEN DATEDIFF(YEAR, DateOfBirth, GETDATE()) BETWEEN 40 AND 49 THEN '40-49' WHEN DATEDIFF(YEAR, DateOfBirth, GETDATE()) BETWEEN 50 AND 59 THEN '50-59' ELSE '60 AND Older' END) AS age_group FROM put your statement**** where ***** group by CASE WHEN DATEDIFF(YEAR, DateOfBirth, GETDATE()) BETWEEN 20 AND 29 THEN '20-29' WHEN DATEDIFF(YEAR, DateOfBirth, GETDATE()) BETWEEN 30 AND 39 THEN '30-39' WHEN DATEDIFF(YEAR, DateOfBirth, GETDATE()) BETWEEN 40 AND 49 THEN '40-49' WHEN DATEDIFF(YEAR, DateOfBirth, GETDATE()) BETWEEN 50 AND 59 THEN '50-59' ELSE '60 AND Older' END
    

0
投票

Image shows the code and its output`

与 k 作为 ( 选择

案件 当年龄 >=18 且年龄
=30 且年龄 < 30 then '18-29' when age>=50 且年龄 < 50 then '30-49' when age> < 60 then '50-60' else '60+' end as AgeGroup, purchase_amount_usd from shopping_trends ) select AgeGroup, sum(purchase_amount_usd) as [Purchases by age group], cast(((sum(purchase_amount_usd)*1.0/(select(sum(purchase_amount_usd)) from k))*100)as decimal(10,2)) as [Purchase% by Age Group] from k group by AgeGroup order by [Purchases by age group] desc

© www.soinside.com 2019 - 2024. All rights reserved.