按年龄范围划分的SQL组

时间:2012-01-06 18:52:06

标签: sql sql-server sql-server-2005 group-by

SQL 2005, 我有一个表'age_c'的表,我需要按年龄范围对记录进行分组。 这是我在这个网站上找到的查询,它让我90%,但'group by'错误,*无效的列名'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

感谢。

6 个答案:

答案 0 :(得分:12)

请尝试这种方式:

 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

答案 1 :(得分:8)

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,因为这是计算列集后执行的唯一子句。

答案 2 :(得分:0)

这是您正在使用的实际代码吗?它看起来不像是因为你错过了34之间的空间。该代码在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

当你完成临时表时,不要忘记放弃临时表

答案 3 :(得分:0)

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

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

或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

答案 4 :(得分:0)

如果您的数据库支持 FILTER WHERE 语法,则可以使用非常简洁的方式对其进行存档:

D:\TEMP>python admin_details.py

< Username >: kapp
< Password >: opiekop

The first line 'lines[0]' in the file is: 'kappa
'The second line 'lines[1]' in the file is: 'opiekop
'

Entered username is: kapp
Entered password is: opiekop

You entered in the wrong username or password.
Try again!

或者如果不是这样的话

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

答案 5 :(得分: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