分区以计算 "collapsed" 数值变量的百分比
Partition to calculate a percentage across a "collapsed" numeric variable
我有一个 table 年龄的人物数据。我想使用 SQL 计算我的 table 中三个或更多折叠年龄类别(例如 18 - 29、30 - 39、40 - 49 等)中的人数百分比。
基本上,我希望我的 table 看起来像这样:
Count | AgeCat | percent
400 | 50 and older | .40
300 | 35 to 49 | .30
300 | Under 35 | .30
这是我的查询:
select count(*),
case when age >= 50 then '50 and older' when age < 50 and age >= 35 then '35 to 49' else 'Under 35' end as AgeCat,
count(*)/sum(count(*)) over (partition by '') as percent
from mydata group by 2;
上面的 window 函数仅在我将我的年龄崩溃限制为两类崩溃(50 岁以上/50 岁以下)时才有效。
这是您的查询:
select count(*),
(case when age >= 50 then '50 and older'
when age < 50 and age >= 35 then '35 to 49'
else 'Under 35'
end) as AgeCat,
count(*)/sum(count(*)) over (partition by '') as percent
from mydata
group by 2;
这应该有效。但是,我会写成:
select count(*),
(case when age >= 50 then '50 and older'
when age >= 35 then '35 to 49'
else 'Under 35'
end) as AgeCat,
( count(*) / sum(count(*)) over () ) as percent
from mydata
group by AgeCat
order by min(age);
我有一个 table 年龄的人物数据。我想使用 SQL 计算我的 table 中三个或更多折叠年龄类别(例如 18 - 29、30 - 39、40 - 49 等)中的人数百分比。
基本上,我希望我的 table 看起来像这样:
Count | AgeCat | percent
400 | 50 and older | .40
300 | 35 to 49 | .30
300 | Under 35 | .30
这是我的查询:
select count(*),
case when age >= 50 then '50 and older' when age < 50 and age >= 35 then '35 to 49' else 'Under 35' end as AgeCat,
count(*)/sum(count(*)) over (partition by '') as percent
from mydata group by 2;
上面的 window 函数仅在我将我的年龄崩溃限制为两类崩溃(50 岁以上/50 岁以下)时才有效。
这是您的查询:
select count(*),
(case when age >= 50 then '50 and older'
when age < 50 and age >= 35 then '35 to 49'
else 'Under 35'
end) as AgeCat,
count(*)/sum(count(*)) over (partition by '') as percent
from mydata
group by 2;
这应该有效。但是,我会写成:
select count(*),
(case when age >= 50 then '50 and older'
when age >= 35 then '35 to 49'
else 'Under 35'
end) as AgeCat,
( count(*) / sum(count(*)) over () ) as percent
from mydata
group by AgeCat
order by min(age);