Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
匹配SQL上月末的值_Sql_Sql Server_Datetime_Left Join - Fatal编程技术网

匹配SQL上月末的值

匹配SQL上月末的值,sql,sql-server,datetime,left-join,Sql,Sql Server,Datetime,Left Join,我基本上有两张表:基金和评级 create table Ratings ( RatingDate date, CustomerNumber int, CustomerName varchar(50), rating varchar(50) ); insert into Ratings (RatingDate,CustomerNumber,CustomerName,Rating) values ( '20200131', 783,'Danny','7+'), ( '20200229'

我基本上有两张表:基金和评级

create table Ratings
(
 RatingDate date,
 CustomerNumber int,
 CustomerName varchar(50),
 rating varchar(50)
 
);

insert into Ratings (RatingDate,CustomerNumber,CustomerName,Rating) values
( '20200131', 783,'Danny','7+'),
( '20200229', 783,'Danny','5'),
( '20200331', 783,'Danny','5'),
( '20200430', 783,'Danny','1'),
( '20200130', 331,'Isabelle','3'),
( '20200229', 331,'Isabelle','3-'),
( '20200330', 331,'Isabelle','2'),
( '20200430', 331,'Isabelle','2'),
( '20200131', 481,'Denise','5'),
( '20200229', 481,'Denise','5-'),
( '20200331', 481,'Denise','6'),
( '20200430', 481,'Denise','6');

select * from Ratings;

RatingDate | CustomerNumber | CustomerName | rating
:--------- | -------------: | :----------- | :-----
2020-01-31 |            783 | Danny        | 7+    
2020-02-29 |            783 | Danny        | 5     
2020-03-31 |            783 | Danny        | 5     
2020-04-30 |            783 | Danny        | 1     
2020-01-30 |            331 | Isabelle     | 3     
2020-02-29 |            331 | Isabelle     | 3-    
2020-03-30 |            331 | Isabelle     | 2     
2020-04-30 |            331 | Isabelle     | 2     
2020-01-31 |            481 | Denise       | 5     
2020-02-29 |            481 | Denise       | 5-    
2020-03-31 |            481 | Denise       | 6     
2020-04-30 |            481 | Denise       | 6     


create table funds
(
 Date_of_Credit date,
 CustomerNumber int,
 CustomerName varchar(50),
 CreditSuffix int,
 CreditAmount money
);

insert into funds (Date_of_Credit,CustomerNumber,CustomerName,CreditSuffix,CreditAmount) values
('20200204', 783, 'Danny',1000,15000),
('20200309', 331, 'Isabelle',1100,27000),
('20200303', 783, 'Danny',1001,25000),
('20200220', 481, 'Denise',2000,17000)
;

select * from funds;

Date_of_Credit | CustomerNumber | CustomerName | CreditSuffix | CreditAmount
:------------- | -------------: | :----------- | -----------: | -----------:
2020-02-04     |            783 | Danny        |         1000 |   15000.0000
2020-03-09     |            331 | Isabelle     |         1100 |   27000.0000
2020-03-03     |            783 | Danny        |         1001 |   25000.0000
2020-02-20     |            481 | Denise       |         2000 |   17000.0000
我需要在信用日期之前将每个客户的信用交易与上个月的评级进行匹配。预期结果如下:

但是,


正如您在链接中看到的我的代码,结果与我的预期不相关。我做错了什么?

假设在每个月的最后一天总是有一个评级,您可以使用eomonth:

要使用示例数据进行此操作,需要将2020年2月28日更改为2020年2月29日,2020年是闰年

另一方面,如果您希望提供上月的最新评级,而不考虑其实际日期,则可以使用横向联接:

select f.*, r.rating
from funds f
outer apply (
    select top (1) r.*
    from ratings r
    where 
        r.CustomerNumber = f.CustomerNumber
        and r.RatingDate >= dateadd(month, -1, datefromparts(year(f.Date_of_Credit), month(f.Date_of_Credit), 1))
        and r.RatingDate <  datefromparts(year(f.Date_of_Credit), month(f.Date_of_Credit), 1)
    order by r.RatingDate desc
) r
-两个查询都产生:

Date_of_Credit | CustomerNumber | CustomerName | CreditSuffix | CreditAmount | rating :------------- | -------------: | :----------- | -----------: | -----------: | :----- 2020-02-04 | 783 | Danny | 1000 | 15000.0000 | 7+ 2020-03-09 | 331 | Isabelle | 1100 | 27000.0000 | 3- 2020-03-03 | 783 | Danny | 1001 | 25000.0000 | 5 2020-02-20 | 481 | Denise | 2000 | 17000.0000 | 5 下面是SQL查询:

 select t1.*, t2.Rating
    from funds t1, Ratings t2
    where t1.CustomerNumber = t2.CustomerNumber and month(t2.RatingDate) = month(t1.Date_of_Credit)-1

看起来很有魅力。我已经添加了我的笔记和你的建议,这将有助于我未来的进步,谢谢。
 select t1.*, t2.Rating
    from funds t1, Ratings t2
    where t1.CustomerNumber = t2.CustomerNumber and month(t2.RatingDate) = month(t1.Date_of_Credit)-1