Skip to content
Advertisement

How to do an exact match on multiple columns and show with the latest date?

I want to only show one occurrences of an exact match of TempString1, TempString2, and TempString3 with the latest date. How can I do an exact match of three fields and only display the row that has the latest date?

Table

TempString1       TempString2    TempString3   Temp4        TempDate
----------------------------------------------------------------------
A                     A2           1            1.01        01/22/2020
A                     A2           1            1.02        01/21/2020
A                     A2           1            1.03        01/20/2020
A                     A2           2            1.01        01/21/2020
A                     A2           2            1.02        01/22/2020
A                     A2           2            1.03        01/20/2020
A                     A2           3            1.01        01/20/2020
A                     A2           3            1.02        01/21/2020
A                     A2           3            1.03        01/23/2020

OUTPUT:

TempString1       TempString2    TempString3   Temp4        TempDate
----------------------------------------------------------------------
A                     A2           1            1.01        01/22/2020
A                     A2           2            1.02        01/22/2020
A                     A2           3            1.03        01/23/2020

Advertisement

Answer

You can try this using ROW_NUMBER() function.

The ROW_NUMBER() is a window function that assigns a sequential integer to each row within a partition of a result set. The row number starts with one for the first row in each partition.

The following shows the syntax of the ROW_NUMBER() function:

ROW_NUMBER() OVER (
    [PARTITION BY partition_expression, ... ]
    ORDER BY sort_expression [ASC | DESC], ...
)

SELECT TempString1
    ,TempString2
    ,TempString3
    ,Temp4
    ,TempDate
FROM (
    SELECT TempString1
        ,TempString2
        ,TempString3
        ,Temp4
        ,TempDate
        ,ROW_NUMBER() OVER (
            PARTITION BY TempString1
            ,TempString2
            ,TempString3
            ORDER BY TempDate DESC
            ) AS RN
    FROM YourTable
    ) a
WHERE rn = 1

Here is the live Demo on db<>fiddle.uk.

create table YourTable(TempString1 Varchar(10), TempString2  Varchar(10), TempString3 int,  Temp4   decimal(18, 2), TempDate date)
insert into YourTable Values('A', 'A2', 1, 1.01, '01/22/2020'),
('A', 'A2', 1, 02, '01/21/2020'),
('A', 'A2', 1, 1.03, '01/20/2020'),
('A', 'A2', 2, 1.01, '01/21/2020'),
('A', 'A2', 2, 1.02, '01/22/2020'),
('A', 'A2', 2, 1.03, '01/20/2020'),
('A', 'A2', 3, 1.01, '01/20/2020'),
('A', 'A2', 3, 1.02, '01/21/2020'),
('A', 'A2', 3, 1.03, '01/23/2020')

 SELECT TempString1
        ,TempString2
        ,TempString3
        ,Temp4
        ,TempDate
        ,ROW_NUMBER() OVER (
            PARTITION BY TempString1
            ,TempString2
            ,TempString3            
            ORDER BY TempDate DESC
            ) AS RN
    FROM YourTable

In below query the result of above query has been considered as table of name a

SELECT TempString1
    ,TempString2
    ,TempString3
    ,Temp4
    ,TempDate
FROM (
    SELECT TempString1
        ,TempString2
        ,TempString3
        ,Temp4
        ,TempDate
        ,ROW_NUMBER() OVER (
            PARTITION BY TempString1
            ,TempString2
            ,TempString3            
            ORDER BY TempDate DESC
            ) AS RN
    FROM YourTable
    ) a
WHERE rn = 1

The result of both the query is as shown below. enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement