在条件下联接创建的表

时间:2019-04-10 13:17:42

标签: sql-server ssms

我正在创建代码以在特定条件下连接两个不同的表。桌子看起来像这样

(TABLE 2)
date | deal_code | originator   | servicer | random |
-----------------------------------------------------
2011 |    001    | commerzbank  |   SPV1   |  1     |
2012 |    001    | commerzbank  |   SPV1   |  12    |
2013 |    001    | commerzbank  |   SPV1   |  7     |
2013 |    005    | unicredit    |   SPV2   |  7     |

和另一个表格

(TABLE 1)
date | deal_code | amount |
---------------------------
2011 |    001    | 100    |
2012 |    001    | 100    |
2013 |    001    | 100    |
2013 |    005    | 200    |

我希望这是最终结果

date | deal_code | amount | originator   | servicer | random |
--------------------------------------------------------------
2013 |    001    | 100    | commerzbank  |   SPV1   |  7     |
2013 |    005    | 200    | unicredit    |   SPV2   |  7     |

我创建了以下代码

select q1.deal_code, q1.date
from table1 q1  
where q1.date = (SELECT MAX(t4.date)
             FROM table1 t4 
             WHERE t4.deal_code = q1.deal_code)

这给了我

(TABLE 3)
date | deal_code | amount |
---------------------------
2013 |    001    | 100    |
2013 |    005    | 200    |

这是对表1的最新观察,现在我想给定始发者和服务者信息,并提供deal_code和日期。有什么建议吗?我希望已经足够清楚。谢谢。

3 个答案:

答案 0 :(得分:0)

这应该做您想要的。命名列时请小心。日期是一个保​​留字,并且太含糊,无法作为专栏的好名字。

declare @Something table
(
    SomeDate int
    , deal_code char(3)
    , originator varchar(20)
    , servicer char(4)
    , random int
)

insert @Something values
(2011, '001', 'commerzbank', 'SPV1', 1)
, (2012, '001', 'commerzbank', 'SPV1', 12)
, (2013, '001', 'commerzbank', 'SPV1', 7)
, (2013, '005', 'unicredit  ', 'SPV2', 7)

declare @SomethingElse table
(
    SomeDate int
    , deal_code char(3)
    , amount int
)

insert @SomethingElse values
(2011, '001', '100')
, (2012, '001', '100')
, (2013, '001', '100')
, (2013, '005', '200')

select x.SomeDate
    , x.deal_code
    , x.originator
    , x.servicer
    , x.random
    , x.amount
from
(
    select s.SomeDate
        , s.deal_code
        , s.originator
        , s.servicer
        , s.random
        , se.amount
        , RowNum = ROW_NUMBER()over(partition by s.deal_code order by s.SomeDate desc)
    from @Something s
    join @SomethingElse se on se.SomeDate = s.SomeDate and se.deal_code = s.deal_code
) x
where x.RowNum = 1

答案 1 :(得分:0)

看起来像这样:

DECLARE @MaxYear INT;

SELECT @MaxYear = MAX(date)
FROM table1 AS t1
    INNER JOIN table2 AS t2
        ON t1.deal_code = t2.deal_code;

SELECT t1.date,
       t1.deal_code,
       t1.amount,
       t2.originator,
       t2.servicer,
       t2.random
FROM table1 AS t1
    INNER JOIN table2 AS t2
        ON t1.date = @MaxYear
           AND t1.deal_code = t2.deal_code;

答案 2 :(得分:0)

对于date列名,我同意Sean Lange的看法。他的方法解决了对相关子查询的依赖性,但实际上,您实际上只需要在现有查询中添加INNER JOIN即可将amount列添加到结果中设置。

select 
  q2.date,
  q2.deal_code, 
  q1.amount,
  q2.originator,    
  q2.servicer,  
  q2.random
from 
  table1 q1  
  join
  table2 q2
    on q1.date = q2.date
    and q1.deal_code = q2.deal_code
where q1.date = (SELECT MAX(t4.date)
             FROM table1 t4 
             WHERE t4.deal_code = q1.deal_code)