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
谢谢。
试试这个方法:
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
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
,因为这是在计算列集之后执行的唯一子句。
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
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
无论如何,您可以使用临时表或嵌套查询。
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
使用完毕后不要忘记删除临时表
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
案件
当年龄 >=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