我目前的查询如下:
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
;
这很好用,我得到以下结果
VAR1 ---------------------------------------------- -Var2 ------------------------------------------------ ------------------------ COCA COLA ZERO CAN 8X330ML ----- 19279 --------------------------------------- --------------------------- BULK VEGETABLES ------------------------ 11723 ----------- ------------ ----------------------------------------- BULK FRUITS --------------------------------- 10496 -------------- ------------------------------------------------ BREAD ------------------------------------------- 6605 ----- -------------------------------------------------- -----------
我的问题是我想添加第三列来计算Var2 / max(Var2)的百分比。请记住,此最大值始终位于第一行(如果有帮助)。
所以我希望我的结果是
VAR1 ---------------------------------------------- Var2 --- Var3
COCA COLA ZERO CAN 8X330ML ----- 19279 -100%
BULK VEGETABLES ------------------------ 11723 --60.81%
BULK FRUITS --------------------------------- 10496-- 54.44%
BREAD ------------------------------------------- 6605-- - 34.26%
我试过
create table Report as
select distinct
a.Var1,
count(distinct b.Var2) as Var2,
calculated Var2/max(calculated Var2)
但我收到错误"不支持以这种方式嵌套的摘要函数。 "
答案 0 :(得分:1)
您始终可以嵌套查询:
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
)