从联接表中获取最新值

时间:2014-02-04 00:54:44

标签: sql-server jointable getlatest

我有两个由映射表连接的表 我想从最后一次Tap中获得强度。

With IssuesCTE AS
(
Select * from Issues
), 
TapsCTE AS 
( 
    SELECT 
        MapIssueTaps.IssueId,
        SD.Id,
        SD.Intensity,
        SD.Notes,
        Row_Number() OVER 
        (
        PARTITION BY MapIssueTaps.IssueId ORDER BY SD.CreatedOn DESC) AS RowNumber
FROM Taps SD
join MapIssueTaps on MapIssueTaps.TapId=SD.Id 
)
Select IssuesCTE.*,TapsCTE.* from IssuesCTE,TapsCTE
join MapIssueTaps on MapIssueTaps.TapId = TapsCTE.Id
--where RowNumber=1

以下是所有数据:

UserId  Id  Name    Intensity   CreatedOn   UpdatedOn   Description IssueId Id  Intensity   Notes   RowNumber
A291B6F4-866D-4EFA-9522-7AA45710C7E0    1   c   2   2014-02-02 03:53:33.200 2014-02-02 03:53:33.200 cx  1   3   8   dd  1
A291B6F4-866D-4EFA-9522-7AA45710C7E0    8   d   5   2014-02-03 22:16:24.157 2014-02-03 22:16:24.157 d   1   3   8   dd  1
A291B6F4-866D-4EFA-9522-7AA45710C7E0    1   c   2   2014-02-02 03:53:33.200 2014-02-02 03:53:33.200 cx  1   2   1   xzc 2
A291B6F4-866D-4EFA-9522-7AA45710C7E0    8   d   5   2014-02-03 22:16:24.157 2014-02-03 22:16:24.157 d   1   2   1   xzc 2
A291B6F4-866D-4EFA-9522-7AA45710C7E0    1   c   2   2014-02-02 03:53:33.200 2014-02-02 03:53:33.200 cx  1   1   8   ss  3
A291B6F4-866D-4EFA-9522-7AA45710C7E0    8   d   5   2014-02-03 22:16:24.157 2014-02-03 22:16:24.157 d   1   1   8   ss  3

我想这样:

UserId  Id  Name    Intensity   CreatedOn   UpdatedOn   Description IssueId Id  Intensity   Notes   RowNumber
A291B6F4-866D-4EFA-9522-7AA45710C7E0    1   c   2   2014-02-02 03:53:33.200 2014-02-02 03:53:33.200 cx  1   3   8   dd  1
A291B6F4-866D-4EFA-9522-7AA45710C7E0    8   d   5   2014-02-03 22:16:24.157 2014-02-03 22:16:24.157 d   1   3   null    dd  1

我该怎么做?

0 个答案:

没有答案