Proc SQL, Aggregate of calculated value

别说谁变了你拦得住时间么 提交于 2019-12-24 12:24:48

问题


My current query is as follows:

create table Report as
select distinct
    a.Var1,
    count(distinct b.Var2) as Var2,

from tbl1 a

inner join tbl2 b
on a.Var3=b.Var3 and a.Var4=b.Var4

inner join tbl3 c
on c.Var3=b.Var3

group by a.Var1
order by Var2 desc
;

This works fine and I get the following result

Var1-----------------------------------------------Var2------------------------------------------------------------------------ COCA COLA ZERO CAN 8X330ML-----19279------------------------------------------------------------------ BULK VEGETABLES------------------------11723---------------------------------------------------------------- BULK FRUITS---------------------------------10496-------------------------------------------------------------- BREAD-------------------------------------------6605------------------------------------------------------------------

My issue is that I want to add a third column that calculates the percentage of Var2/max(Var2). Keep in mind that this max is always in the first row (if that helps).

So I want my result to be

Var1----------------------------------------------Var2 ---Var3

COCA COLA ZERO CAN 8X330ML-----19279 -100%

BULK VEGETABLES------------------------11723 --60.81%

BULK FRUITS---------------------------------10496-- 54.44%

BREAD-------------------------------------------6605--- 34.26%

I tried

create table Report as
   select distinct
   a.Var1,
   count(distinct b.Var2) as Var2,
   calculated Var2/max(calculated Var2)

but I get the error "Summary functions nested in this way are not supported. "


回答1:


You can always nested queries:

create table Report as  
select  
    Var1,  
    Var2,  
    Var2/max(Var2) as pct_Var2  
from (  
    select distinct  
        a.Var1,  
        count(distinct b.Var2) as Var2  
    from  
        (...)  
    group by a.Var1  
)  


来源:https://stackoverflow.com/questions/31983321/proc-sql-aggregate-of-calculated-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!