简体   繁体   中英

Window function after aggregation

I'm making a report, which includes window function. I have to sum values by row and show total by manufacturer, but I get different sums:

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

I assume, it's due to grouping packs, but without it I get error. How do I solve this?

Thanks in advance!

Use 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;

However, if you just need the total by manufacturer, perhaps you don't need window functions at all

select manufacturer, sum(packs) as manufacturer_sum
from staging.sales
group by manufacturer;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM