Getting latest value from joined table

56 Views Asked by At

I have 2 tables joined by a mapping table I want to get the the intensity from the last 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

Here is all the data:

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

I would like this:

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

How can I do it?

0

There are 0 best solutions below