0
您好我有,如果交易被處理其中有賬戶,yearmonth和貿易計數SQL滾動3個月總有Yearmonth
account Yearmonth Trade_count
XXXXX 201701 1
XXXXX 201701 0
XXXXX 201702 1
XXXXX 201703 1
XXXXX 201704 1
XXXXX 201704 1
XXXXX 201705 1
的trade_count爲1的數據集和0,如果沒有處理
我想輸出這樣
Account Yearmonth Total_Trades_Month past_3_month trades
XXXXX 201703 1 3
XXXXX 201704 2 3
XXXXX 201705 1 4
到目前爲止,我已經試過:
select yearmonth, (yearmonth - 3) as 'ym',
SUM(Case when COMMISSION is not null and Commission > 0 then Trade_Count Else 0 END) as 'TotalTrades',
sum(CASE when yearmonth between (yearmonth - 3) and yearmonth and commission > 0 then Trade_Count else 0 end) as 'rolling'
sum(Trade_Count)over(partition by yearmonth)
FROM WEALTHDB.DBO.WF_PM_DOR_DB
group by yearmonth
order by yearmonth
請注意,yearmonth只是一個整數,並沒有編碼爲日期。任何幫助表示讚賞
哪有失蹤幾個月? –
您正在使用哪個SQL?現代SQL具有__window函數___,它對滾動總計非常理想。我假設'Yearmonth'是你自己的設備? – Manngo
如果'yearmonth'是一個整數,那麼比較是否存在問題,比如'201611 + 3'和'201701'? – Manngo