我們有一個資料倉庫,過去 14 年來我們一直在其中存盤資料。我使用的是 SQL Server 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
uj5u.com熱心網友回復:
關閉您的示例資料應該可以。
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/379301.html
標籤:sql sql-server 查询语句
上一篇:SQLSum-然后加入-然后評論
