SQL Server中两个表之间的数据比较

时间:2015-04-27 09:46:47

标签: sql sql-server sql-server-2008 sql-server-2008-r2

我要求我有两个表tbl_bbtbl_rr,其中的数据如下:

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

isin    agency  rr_rating   rr_date
------------------------------------
142356  MDA     A         2010-01-15
142356  MDA     AA+       2009-05-30
142356  MDA     BBB       2013-07-04

现在,要求我必须从tbl_bb中选择每一条记录,并根据isin和agency的加入循环tbl_rr中的数据,并且循环的条件是从tbl_bb考虑日期并检查每个日期tbl_rr选择最接近tbl_bb日期的日期,其他条件为rr_date(tbl_rr) <= date (tbl_bb)

输出:

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

谢谢!

3 个答案:

答案 0 :(得分:1)

我会使用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;

完整的工作示例

-- 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;

答案 1 :(得分:0)

我要做的是我会按照说明中的说明加入这些表格,在两个日期之间找到DATEDIFF,然后找到每个DATEDIFFISIN的最低agency值}

答案 2 :(得分:0)

根据我的假设,如果你想过滤我们可以实现的数据,我们可以这样继续进行,如

C.RN - 1 = CC.RR or   C.RN - 1 > CC.RR OR C.RN - 1 < CC.RR

代码:

  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