2013-12-12 113 views
1

我正在使用Teradata 我有一個表格顯示每天的銷售數字。 我想要做這樣的事情按日期劃分的SQL分區,日期總和

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子句。有人可以幫忙嗎?我搜索了互聯網,但找不到我在找什麼。

回答

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)