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:
UniqueID | timestamp | other columns ... |
---|---|---|
1 | 2022-02-15 00:00:00.000 | ... |
... | 2022-02-15 00:00:00.000 | ... |
54364 | 2023-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 Name | Last Timestamp |
---|---|
Table 1 | 2023-04-11 00:00:00.000 |
Table 2 | 2023-04-12 00:00:00.000 |
Table 3 | 2023-04-12 00:00:00.000 |
Table 4 | 2023-04-12 00:00:00.000 |
Table 5 | 2023-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!
1条答案
按热度按时间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 inunion all
members, so we need subqueries. Assuming that all of your tables have columnsts
(timestamp) anduniqueID
(for ordering):