简体   繁体   English

SQL按日期分区,按日期求和

[英]SQL partition by dates, sum by dates

I'm using Teradata I have a table which shows me sales numbers by each day. 我正在使用Teradata,我有一张表,该表显示每天的销售量。 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. 我似乎找不到如何使用where子句进行总结。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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