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

时间:2013-12-12 21:19:51

标签: sql date

我正在使用Teradata 我有一张桌子,每天都会显示销售数字。 我想做像

这样的事情
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)

我似乎无法找到我如何使用where子句进行求和。有人可以帮忙吗我在互联网上搜索但找不到我要找的东西。

1 个答案:

答案 0 :(得分:2)

尝试使用条件聚合:

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)