0
我有一個創建的視圖,我希望在交易日期中顯示天數的增量。 這裏是我的表+ SQL語句的某些行:日期具有多個天數的差異返回負數
SELECT
FirstAppearenceDate
,TranDate
,CountryName
,AvgTran
,RunningTotal
,ISNULL(RunningTotal - LAG(RunningTotal, 1)
OVER (PARTITION BY CountryName ORDER BY CountryName, TranDate),0) AS MoneyDelta
,ISNULL(DATEDIFF(day,LAG(CONVERT(DATETIME, TranDate, 103), 1) OVER
(ORDER BY CountryName, TranDate),
CONVERT(DATETIME, TranDate, 103)),0) AS DaysDelta
FROM LTV_Preprocessing_2
ORDER BY CountryName, TranDate
,其結果是:
[前30行]:
FirstAppearenceDate| TranDate| CountryName| AvgTran| RunningTotal| MoneyDelta | DaysDelta
20150510 20150510 Afghanistan 50.0000000000 50.0000000000 0.0000000000 0
20150510 20150514 Afghanistan 22.0000000000 72.0000000000 22.0000000000 4
20150510 20150603 Afghanistan 40.0000000000 112.0000000000 40.0000000000 20
20150609 20150609 Afghanistan 25.0000000000 137.0000000000 25.0000000000 6
20150718 20150718 Afghanistan 27.5500000000 164.5500000000 27.5500000000 39
20150718 20150727 Afghanistan 135.6500000000 300.2000000000 135.6500000000 9
20151026 20151026 Afghanistan 10.0000000000 310.2000000000 10.0000000000 91
20151214 20151214 Afghanistan 20.0000000000 330.2000000000 20.0000000000 49
20150510 20160102 Afghanistan 10.0000000000 340.2000000000 10.0000000000 19
20150718 20160122 Afghanistan 18.5000000000 358.7000000000 18.5000000000 20
20150510 20160616 Afghanistan 47.0000000000 405.7000000000 47.0000000000 146
20140806 20140806 Aland Islands 10.0000000000 10.0000000000 0.0000000000 -680
20140808 20140808 Aland Islands 20.0000000000 30.0000000000 20.0000000000 2
20140816 20140816 Aland Islands 27.7100000000 57.7100000000 27.7100000000 8
20140806 20140819 Aland Islands 10.0000000000 67.7100000000 10.0000000000 3
20140820 20140820 Aland Islands 90.3300000000 158.0400000000 90.3300000000 1
20140820 20140823 Aland Islands 15.0800000000 173.1200000000 15.0800000000 3
20140826 20140826 Aland Islands 37.9000000000 211.0200000000 37.9000000000 3
20140912 20140912 Aland Islands 3.0800000000 214.1000000000 3.0800000000 17
20140919 20140919 Aland Islands 20.0000000000 234.1000000000 20.0000000000 7
20140820 20140820 Albania 79.0400000000 79.0400000000 0.0000000000 -30
20140821 20140821 Albania 22.0000000000 101.0400000000 22.0000000000 1
20140821 20140822 Albania 20.0000000000 121.0400000000 20.0000000000 1
20140821 20140824 Albania 50.0000000000 171.0400000000 50.0000000000 2
20140821 20140827 Albania 40.0000000000 211.0400000000 40.0000000000 3
20140821 20140828 Albania 48.0000000000 259.0400000000 48.0000000000 1
20140831 20140831 Albania 20.0000000000 279.0400000000 20.0000000000 3
20140821 20140901 Albania 80.0000000000 359.0400000000 80.0000000000 1
20140821 20140902 Albania 119.0000000000 478.0400000000 119.0000000000 1
20140821 20140910 Albania 30.0000000000 508.0400000000 30.0000000000 8
看底片,我想我的日三角洲的行爲與貨幣三角洲完全相同 - 對於每個國家,它以0開頭。在這裏您可以看到它在日內達爾文欄中的阿富汗結束和奧蘭羣島開始之間確實存在差異。
在此先感謝!
你的意思是關於** **消除?,你要負的數據轉換成正的數據?如果是,請使用** ABS **功能。 –
我希望這個窗口函數能像我的貨幣三角洲那樣「理解」,對於每個國家,它應該從天數增量列中的零開始。它在第一個國家隊列中工作,但之後它在一個國家的結束和下一個第一個轉換日期之間有區別。 –