简体   繁体   English

如何提取上个月的数据

[英]How can I pull previous month data

We have a data warehouse where we have been storing data for the past 14 years.我们有一个数据仓库,过去 14 年来我们一直在其中存储数据。 I am using SQL server 2016 version.我正在使用 SQL 服务器 2016 版。 It is personal information data.它是个人信息数据。 This is basically the data regarding the deposits that will contains columns like for example: MemberID, membershipDate, MonthlyDepositdate, DepositAmount.这基本上是有关存款的数据,其中将包含以下列:MemberID、membershipDate、MonthlyDepositdate、DepositAmount。

Until 5 years back the member info was updated yearly into the DW but from the last 5 years the data is pulled monthly and appended to dw regularly.直到 5 年前,会员信息每年都会更新到 DW 中,但从过去 5 年开始,每月都会提取数据并定期附加到 dw。

Now, my problem is that some member who joined before last 5 years back has the monthly deposit date with yearly updated date for example: 2011-01-01;现在,我的问题是一些在过去 5 年前加入的会员有每月存款日期和每年更新的日期,例如:2011-01-01; 2012-12-31, 2013-12-31 etc... but from last 5 years we storing data every month so the monthly deposit date will be something like below: 2017-01-31; 2012-12-31, 2013-12-31 等...但是从过去 5 年开始,我们每个月都存储数据,因此每月存款日期如下:2017-01-31; 2017-02-28; 2017-02-28; 2017-03-31 etc... and the corresponding deposit amount. 2017-03-31等...以及相应的存款金额。

So now I have a requirement to pull the data for the previous month deposit amount along with the current month deposit amount.所以现在我需要提取上个月存款金额的数据以及当前月份的存款金额。 For this requirement I can simply do it by using EOMONTH(DATEADD,-1, Monthly Deposit Date) but as I said until the past 5 years the data we stored is yearly based.对于这个要求,我可以简单地使用EOMONTH(DATEADD,-1, Monthly Deposit Date)来完成,但正如我所说,直到过去 5 年,我们存储的数据都是基于年度的。 How to achieve this situation please, your help is very much appreciated.请如何实现这种情况,非常感谢您的帮助。 Please Note:- you might have observed the data until 2016 is appended every year and from 2017 onwards the data is saved by month.请注意:-您可能已经观察到,直到 2016 年的数据每年都会添加,从 2017 年开始,数据会按月保存。

My original dw table:我原来的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

I need to produce the result something like below:我需要产生如下结果:

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

Going off your sample data something like this should work.像这样关闭您的示例数据应该可以。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM