Select most recent timestamp per record from multiple joined tables

Andreas Schwibach 1 Reputation point
2022-01-29T09:58:53.617+00:00

I want to create a view on multiple joined tables.
Each of the tables has a timestamp.
The different records in the source tables may be updated at different times.

So, in the view I would like to select the most recent timestamp for each record from the different timestamp columns across the joined tables.
Is there a way to do a sort of max function on these fields?

Thanks!

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,774 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 112.5K Reputation points
    2022-01-29T11:55:17.217+00:00

    I think that it is possible. For your details, consider one of approaches:

    create view MyView
    as
        with S1 as
        (
            select *, row_number() over (partition by main_id order by [timestamp] desc) as n from SecondaryTable1
        ),
        S2 as
        (
            select *, row_number() over (partition by main_id order by [timestamp] desc) as n from SecondaryTable2
        )
        select MainTable.id -- ... the list of output columns from three tables
        from MainTable
        inner join S1 on S1.main_id = MainTable.id
        inner join S2 on S2.main_id = MainTable.id
        where S1.n = 1 and S2.n = 1
    

    It shows one main table and two secondary joined tables, that have foreign keys (main_id) and timestamps.

    0 comments No comments

  2. Erland Sommarskog 101.4K Reputation points MVP
    2022-01-29T13:14:22.49+00:00

    I think I interpret your question differently from Viorel.

    From my interpretation, this is very easy to do if you are on Azure SQL Database or, I assume, Azure SQL Managed Instance:

    SELECT ... greatest(t1.timestamp, t2.timestamp, t3.timestamp, ...)
    FROM  ...
    

    Alas, the greatest function is not available in any on-premises version of SQL Server. In that case you can do:

    CROSS APPLY (SELECT MAX(V.timestamp) AS maxtimestamp
          FROM (VALUES(t1.timestamp), (t2.timestamp), (t3.timestamp)) AS V(timestamp)
        ) AS  M
    

    As I said, Viorel and I seems to interpret your question differently. It could help if you post CREATE TABLE + INSERT statements with sample data and the desired result given the sample. This helps to clarify what you are looking for and it makes it easy to copy and paste into a query window to develop a tested solution. It also helps if you include the output of "SELECT @@version", so we know your presumptions.

    0 comments No comments

  3. YufeiShao-msft 7,061 Reputation points
    2022-01-31T08:11:50.25+00:00

    Hi @Andreas Schwibach ,

    Check out this similar thread,
    such as, select columns that are in the group or used in an aggregate function and use a join to get this working

    select s1.*   
    from sensorTable s1  
    inner join   
    (  
      SELECT sensorID, max(timestamp) as mts  
      FROM sensorTable   
      GROUP BY sensorID   
    ) s2 on s2.sensorID = s1.sensorID and s1.timestamp = s2.mts  
    

    -------------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments