我正在尝试计算一列中二级总列的百分比。
我写:
create temporary table screenings_count_2018 as
select guid,
datepart(y, screening_screen_date) as year,
sum(case when screening_package = 4 then 1 end) as count_package_4,
sum(case when screening_package = 3 then 1 end) as count_package_3,
sum(case when screening_package = 2 then 1 end) as count_package_2,
sum(case when screening_package = 1 then 1 end) as count_package_1,
sum(case when screening_package in (1, 2, 3, 4) then 1 end) as count_total_packages
from prod.leasing_fact
where year = 2018
group by guid, year;
该表确定了初始计数和总计数列。所有列看起来都正确。
然后,我使用ratio_to_report来计算百分比(引用this教程):
create temporary table screenings_percentage as
select
guid,
year,
ratio_to_report(count_package_1) over (partition by count_total_packages) as percentage_package_1
from screenings_count_2018
group by guid, year,count_package_1,count_total_packages
order by percentage_package_1 desc;
我也尝试过:
select
guid,
year,
sum(count_package_1/count_total_packages) as percentage_package_1
-- ratio_to_report(count_package_1) over (partition by count_total_packages) as percentage_package_1
from screenings_count_2018
group by guid, year,count_package_1,count_total_packages
order by percentage_package_1 desc;
不幸的是,percentage_package_1只返回所有空值(这不正确 - 我期待百分比)。两者都没有工作。
我究竟做错了什么?
谢谢!
既然你已经列出了包含组件和总数的列,在创建screenings_count_2018
时,你真的需要使用ratio_to_report
吗?
select
, guid
, year
, count_package_1/count_total_packages as percentage_package_1
, count_package_2/count_total_packages as percentage_package_2
, count_package_3/count_total_packages as percentage_package_3
, count_package_4/count_total_packages as percentage_package_4
from screenings_count_2018
这应该工作。 NB你保证永远不会有count_total_packages
为零吗?如果它可以为零,则需要处理它。一种方法是使用案例陈述。
如果你希望每个包的百分比出现在一个列中,那么你可以使用ratio_to_report
- 它是一个“窗口”分析函数,对于原始表它将是这样的。
with count_table as (
select guid
, datepart(y, screening_screen_date) as year
, screening_package
, count(1) as count
from prod.leasing_fact
where year = 2018
group by guid
, datepart(y, screening_screen_date)
, screening_package
)
select guid
, year
, screening_package
, ratio_to_report(count) over(partition by guid, year, screening_package) as perc_of_total
from count_table
你需要round(100.0*count_package_1/count_total_packages,1)
等等,因为你已经计算了小计和总数