我正在制作报告,其中包括窗口功能。 我必须按行对值求和,并按制造商显示总数,但是得到的总和却不同:
select manufacturer,
product_code,
date_code,
sum(packs),
sum(packs) over(partition by manufacturer,date_code)
from staging.sales
group by manufacturer,
product_code,
date_code,
packs
我认为,这是由于分组组合引起的,但是如果没有它,我会出错。我该如何解决?
谢谢!
答案 0 :(得分:0)
使用sum(sum(packs))
:
select manufacturer, product_code, date_code,
sum(packs),
sum(sum(packs)) over (partition by manufacturer) as manufacturer_sum
from staging.sales
group by manufacturer, product_code, date_code;
但是,如果您仅需要按制造商分类的总数,则可能根本不需要窗口功能
select manufacturer, sum(packs) as manufacturer_sum
from staging.sales
group by manufacturer;