[英]How can I pull previous month data
我們有一個數據倉庫,過去 14 年來我們一直在其中存儲數據。 我正在使用 SQL 服務器 2016 版。 它是個人信息數據。 這基本上是有關存款的數據,其中將包含以下列:MemberID、membershipDate、MonthlyDepositdate、DepositAmount。
直到 5 年前,會員信息每年都會更新到 DW 中,但從過去 5 年開始,每月都會提取數據並定期附加到 dw。
現在,我的問題是一些在過去 5 年前加入的會員有每月存款日期和每年更新的日期,例如:2011-01-01; 2012-12-31, 2013-12-31 等...但是從過去 5 年開始,我們每個月都存儲數據,因此每月存款日期如下:2017-01-31; 2017-02-28; 2017-03-31等...以及相應的存款金額。
所以現在我需要提取上個月存款金額的數據以及當前月份的存款金額。 對於這個要求,我可以簡單地使用EOMONTH(DATEADD,-1, Monthly Deposit Date)來完成,但正如我所說,直到過去 5 年,我們存儲的數據都是基於年度的。 請如何實現這種情況,非常感謝您的幫助。 請注意:-您可能已經觀察到,直到 2016 年的數據每年都會添加,從 2017 年開始,數據會按月保存。
我原來的dw表:
MemberID, membershipDate, MonthlyDepositdate, DepositAmount.
111 2003-02-15 2003-12-31 53.00
111 2003-02-15 2004-12-31 101.00
111 2003-02-15 2005-12-31 162.00
... .... ... ...
111 2003-02-15 2017-01-31 1650.00
111 2003-02-15 2017-02-28 1660.00
111 2003-02-15 2017-03-31 1672.00
222 2014-05-19 2014-12-31 30.00
222 2014-05-19 2015-12-31 72.00
222 2014-05-19 2016-12-31 113.00
222 2014-05-19 2017-01-31 115.00
222 2014-05-19 2017-02-28 120.00
222 2014-05-19 2017-03-31 123.00
我需要產生如下結果:
MemberID, membershipDate, MonthlyDepositdate, DepositAmount, PreviousMonthDepositDate, PreviousmonthDepositAmt
111 2003-02-15 2003-12-31 53.00 ___ 0.0
111 2003-02-15 2004-12-31 101.00 2003-12-31 53.00
111 2003-02-15 2005-12-31 162.00 2004-12-31 101.00
... .... ... ... .... ...
111 2003-02-15 2017-01-31 1650.00 2016-12-31 1600.00
111 2003-02-15 2017-02-28 1660.00 2017-01-31 1650.00
111 2003-02-15 2017-03-31 1672.00 2017-02-28 1660.00
222 2014-05-19 2014-12-31 30.00 ___ 0.0
222 2014-05-19 2015-12-31 72.00 2014-12-31 30.00
222 2014-05-19 2016-12-31 113.00 2015-12-31 72.00
222 2014-05-19 2017-01-31 115.00 2016-12-31 113.00
222 2014-05-19 2017-02-28 120.00 2017-01-31 115.00
222 2014-05-19 2017-03-31 123.00 2017-02-28 120.00
像這樣關閉您的示例數據應該可以。
declare @Something table
(
MemberID int
, membershipDate date
, MonthlyDepositdate date
, DepositAmount decimal(7,2)
)
insert @Something
select 111, '2003-02-15', '2003-12-31', 53.00 union all
select 111, '2003-02-15', '2004-12-31', 101.00 union all
select 111, '2003-02-15', '2005-12-31', 162.00 union all
select 111, '2003-02-15', '2017-01-31', 1650.00 union all
select 111, '2003-02-15', '2017-02-28', 1660.00 union all
select 111, '2003-02-15', '2017-03-31', 1672.00 union all
select 222, '2014-05-19', '2014-12-31', 30.00 union all
select 222, '2014-05-19', '2015-12-31', 72.00 union all
select 222, '2014-05-19', '2016-12-31', 113.00 union all
select 222, '2014-05-19', '2017-01-31', 115.00 union all
select 222, '2014-05-19', '2017-02-28', 120.00 union all
select 222, '2014-05-19', '2017-03-31', 123.00
select MemberID
, membershipDate
, MonthlyDepositdate
, DepositAmount
, PreviousMonthDepositDate = lag(MonthlyDepositDate, 1) over(partition by MemberID order by MonthlyDepositDate)
, PreviousmonthDepositAmt = lag(DepositAmount, 1) over(partition by MemberID order by MonthlyDepositDate)
from @Something
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.