简体   繁体   中英

SQL partition by dates, sum by dates

I'm using Teradata I have a table which shows me sales numbers by each day. I want to do something like

select product
    , sum(sales) over (partition by product) where dates between date '2013-10-01' and date '2013-12-12' as Q4Sales
    , sum(sales) over (partition by product) where dates between date '2013-07-01' and date '2013-10-31' as Q3Sales
    from tablex
    qualify 
      row_number()
         over(partition by dates, product)

I can't seem to find how I do this summing with where clause. Can someone help. I've searched the internet but can't find what I'm looking for.

Try using conditional aggregation:

select product,
       sum(case when dates between date '2013-10-01' and date '2013-12-12' then sales end) over
           (partition by product
           ) as Q4Sales
       sum(case when dates between date '2013-07-01' and date '2013-10-31' then sales end) over
           (partition by product
           ) as Q3Sales
from tablex
qualify row_number() over(partition by dates, product)

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