[英]Data comparison between two tables in SQL server
I have a requirement where I have two tables tbl_bb
and tbl_rr
and data in them like this: 我有一个要求,其中我有两个表
tbl_bb
和tbl_rr
并且其中有数据,如下所示:
tbl_bb : tbl_bb :
isin agency rating date
----------------------------------
142356 MDA A 2010-01-20
142356 MDA AA 2012-05-14
142356 MDA BB 2013-07-04
tbl_rr : tbl_rr :
isin agency rr_rating rr_date
------------------------------------
142356 MDA A 2010-01-15
142356 MDA AA+ 2009-05-30
142356 MDA BBB 2013-07-04
Now, requirement is I have to pick each and every record from tbl_bb and loop through the data in tbl_rr based on joining of isin and agency and the condition for looping is consider date from tbl_bb and check every date from tbl_rr
and pick the closest date to that of tbl_bb
date and other condition is rr_date(tbl_rr) <= date (tbl_bb)
现在的要求是我要挑从tbl_bb和环通基于ISIN和代理和循环条件的加盟tbl_rr数据的每一个记录是从tbl_bb考虑日期和检查每个日期
tbl_rr
,并挑选最接近的日期tbl_bb
日期和其他条件的日期是rr_date(tbl_rr) <= date (tbl_bb)
Output: 输出:
isin agency rating date rr_rating rr_date
-------------------------------------------------------
142356 MDA A 2010-01-20 A 2010-01-15
142356 MDA AA 2012-05-14 AA+ 2010-01-15
142356 MDA BB 2013-07-04 BBB 2013-07-04
Thanks!! 谢谢!!
I would do this using APPLY
to get the closest 1 record from tbl_rr
: 我将使用
APPLY
来执行此操作,以从tbl_rr
获得最近的1条记录:
SELECT bb.isin,
bb.agency,
bb.rating,
bb.date,
rr.rr_rating,
rr.rr_date
FROM tbl_bb AS bb
OUTER APPLY
( SELECT TOP 1 rr.rr_rating, rr.rr_date
FROM tbl_rr AS rr
WHERE rr.isin = bb.isin
AND rr.agency = bb.agency
AND rr.rr_date <= bb.date
ORDER BY rr.rr_date DESC
) AS rr;
FULL WORKING EXAMPLE 完整的示例
-- SAMPLE DATA
WITH tbl_bb AS
( SELECT isin, agency, rating, date
FROM (VALUES
(142356,'MDA','A','2010-01-20'),
(142356,'MDA','AA','2012-05-14'),
(142356,'MDA','BB','2013-07-04')
) AS bb(isin, agency, rating, date)
), tbl_rr AS
( SELECT isin, agency, rr_rating, rr_date
FROM (VALUES
(142356,'MDA','A','2010-01-15'),
(142356,'MDA','AA+','2012-05-30'),
(142356,'MDA','BBB','2013-07-04')
) AS rr (isin, agency, rr_rating, rr_date)
)
-- SAMPLE DATA END
SELECT bb.isin,
bb.agency,
bb.rating,
bb.date,
rr.rr_rating,
rr.rr_date
FROM tbl_bb AS bb
OUTER APPLY
( SELECT TOP 1 rr.rr_rating, rr.rr_date
FROM tbl_rr AS rr
WHERE rr.isin = bb.isin
AND rr.agency = bb.agency
AND rr.rr_date <= bb.date
ORDER BY rr.rr_date DESC
) AS rr;
我要做的是按照说明中的说明加入这些表,找到两个日期之间的DATEDIFF
,然后只查找每个ISIN
和agency
最低DATEDIFF
值
As per my assumption we can proceed like this if you want filter the data we can achieve like 根据我的假设,如果您想要过滤数据,我们可以像这样继续进行
C.RN - 1 = CC.RR or C.RN - 1 > CC.RR OR C.RN - 1 < CC.RR
Code: 码:
declare @t table (ISIN INT,Agency varchar(10),Rating varchar(5),dated date)
insert into @t (ISIN,Agency,Rating,dated)values (142356,'MDA','A','2010-01-20'),
(142356,'MDA','AA','2012-05-14'),
(142356,'MDA','BB','2013-07-04')
declare @tt table (ISIN INT,Agency varchar(10),Rating varchar(5),dated date)
insert into @tt (ISIN,Agency,Rating,dated)values (142356,'MDA','A','2010-01-15'),
(142356,'MDA','AA+','2012-05-30'),
(142356,'MDA','BBB','2013-07-04')
;with cte as
(select ISIN,
Agency,
Rating,
dated,
ROW_NUMBER()OVER(ORDER BY ISIN)RN
from @t)
, cte1 as
(select
ISIN,
Agency,
Rating,
dated,
ROW_NUMBER()OVER(ORDER BY ISIN)RR
from @tt)
select C.ISIN,C.Agency,
C.Rating,C.dated,
CC.Agency,CC.Rating,
CC.dated from cte c
INNER JOIN cte1 CC
ON C.RN = CC.RR
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.