计算总数的百分比-Redshift / SQL

时间:2019-04-25 18:51:33

标签: sql amazon-redshift percentage

我正在尝试计算一列在第二总列中所占的百分比。

我写道:

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;

不幸的是,percent_package_1只返回所有空值(这是不正确的-我期望百分比)。都没有工作。

我在做什么错?

谢谢!

2 个答案:

答案 0 :(得分:0)

由于您已经列出了包含组件和总数的列,因此在创建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

应该可以。注意:您是否可以保证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

答案 1 :(得分:0)

您将需要round(100.0*count_package_1/count_total_packages,1),依此类推,因为您已经计算了小计和总计

相关问题