SQL Server Combine last values from timestamp columns across several tables - into a single table?

cl25kdpy  于 2023-06-21  发布在  其他
关注(0)|答案(1)|浏览(153)

There are several tables in the database each containing a column with the same name, "timestamp":

Table 1:
| UniqueID | timestamp | other columns ... |
| ------------ | ------------ | ------------ |
| 1 | 2022-02-15 00:00:00.000 | ... |
| ... | 2022-02-15 00:00:00.000 | ... |
| 43374082 | 2023-04-11 00:00:00.000 | ... |

Table 2:

UniqueIDtimestampother columns ...
12022-02-15 00:00:00.000...
...2022-02-15 00:00:00.000...
543642023-04-12 00:00:00.000...

... and a couple more similar tables.

How does one go about taking the last row's timestamps from each table and combining them in a table like this?

Combo table:
| Table Name | last timestamp |
| ------------ | ------------ |
| Table 1 | 2023-04-11 00:00:00.000 |
| Table 2 | 2023-04-12 00:00:00.000 |

The ultimate goal is to check several tables in a database for how long ago they were last updated (last row's timestamps), and display these timestamps in a readable (and scalable and sortable) format.

The code how to get the last timestamps from each table it likely something like this:

SELECT
    TOP 1 timestamp
    FROM [Table 1]
    ORDER BY UniqueID DESC

... yet I am not sure how to put these timestamps into a single table together with source table names.

Edit 1:

Someone or something came up with this code that does what I need:

WITH #CTE_Table1 AS (
    SELECT 'Table 1' AS [Table Name], timestamp, ROW_NUMBER() OVER (ORDER BY UniqueID DESC) AS RowNum
    FROM [Table 1]
),
#CTE_Table2 AS (
    SELECT 'Table 2' AS [Table Name], timestamp, ROW_NUMBER() OVER (ORDER BY UniqueID DESC) AS RowNum
    FROM [Table 2]
),
#CTE_Table3 AS (
    SELECT 'Table 3' AS [Table Name], timestamp, ROW_NUMBER() OVER (ORDER BY UniqueID DESC) AS RowNum
    FROM [Table 3]
),
#CTE_Table4 AS (
    SELECT 'Table 4' AS [Table Name], timestamp, ROW_NUMBER() OVER (ORDER BY UniqueID DESC) AS RowNum
    FROM [Table 4]
),
#CTE_Table5 AS (
    SELECT 'Table 5' AS [Table Name], timestamp, ROW_NUMBER() OVER (ORDER BY UniqueID DESC) AS RowNum
    FROM [Table 5]
)

SELECT [Table Name], [Last Timestamp]
FROM (
    SELECT [Table Name], timestamp AS [Last Timestamp], RowNum
    FROM #CTE_Table1
    WHERE RowNum = 1

    UNION ALL

    SELECT [Table Name], timestamp AS [Last Timestamp], RowNum
    FROM #CTE_Table2
    WHERE RowNum = 1

    UNION ALL

    SELECT [Table Name], timestamp AS [Last Timestamp], RowNum
    FROM #CTE_Table3
    WHERE RowNum = 1

    UNION ALL

    SELECT [Table Name], timestamp AS [Last Timestamp], RowNum
    FROM #CTE_Table4
    WHERE RowNum = 1

    UNION ALL

    SELECT [Table Name], timestamp AS [Last Timestamp], RowNum
    FROM #CTE_Table5
    WHERE RowNum = 1

) AS [Last Timestamp];

Output:

Table NameLast Timestamp
Table 12023-04-11 00:00:00.000
Table 22023-04-12 00:00:00.000
Table 32023-04-12 00:00:00.000
Table 42023-04-12 00:00:00.000
Table 52023-04-12 00:00:00.000

... which works - yet I am not happy with it: from the little I know of SQL, the code doesn't seem to be of high quality or easily readable.

Thank you!

sh7euo9m

sh7euo9m1#

You can use union all , and pass the table name as a literal value.

It is a bit tricky because in SQL Server we can't use order by directly in union all members, so we need subqueries. Assuming that all of your tables have columns ts (timestamp) and uniqueID (for ordering):

select 'table1' as table_name, (select top 1 ts from table1 order by uniqueId desc) as last_ts
union all select 'table2',     (select top 1 ts from table2 order by uniqueId desc)
union all select 'table3',     (select top 1 ts from table3 order by uniqueId desc)

相关问题