合并多个表中时间戳列的最后数值,合并为单个表格?

huangapple go评论59阅读模式
英文:

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

问题

数据库中有几个表,每个表中都包含一个同名列 "timestamp":

表1:

唯一标识 时间戳 其他列 ...
1 2022-02-15 00:00:00.000 ...
... 2022-02-15 00:00:00.000 ...
43374082 2023-04-11 00:00:00.000 ...

表2:

唯一标识 时间戳 其他列 ...
1 2022-02-15 00:00:00.000 ...
... 2022-02-15 00:00:00.000 ...
54364 2023-04-12 00:00:00.000 ...

... 还有几个类似的表。

如何获取每个表中 最后一行的时间戳 并将它们合并到如下的表中?

合并表:

表名 最后时间戳
表1 2023-04-11 00:00:00.000
表2 2023-04-12 00:00:00.000

最终目标是检查数据库中几个表的最近更新时间(最后一行的时间戳),并以可读(可扩展和可排序)的格式显示这些时间戳。

获取每个表的最后时间戳的代码可能是这样的:

SELECT
	TOP 1 时间戳
	FROM [1]
	ORDER BY 唯一标识 DESC

... 但我不确定如何将这些时间戳放入单个表中,并包括源表名称。


编辑1:

有人或某物提出了可以满足我的需求的代码:

WITH #CTE_表1 AS (
    SELECT '表1' AS [表名], 时间戳, ROW_NUMBER() OVER (ORDER BY 唯一标识 DESC) AS RowNum
    FROM [1]
),
#CTE_表2 AS (
    SELECT '表2' AS [表名], 时间戳, ROW_NUMBER() OVER (ORDER BY 唯一标识 DESC) AS RowNum
    FROM [2]
),
#CTE_表3 AS (
    SELECT '表3' AS [表名], 时间戳, ROW_NUMBER() OVER (ORDER BY 唯一标识 DESC) AS RowNum
    FROM [3]
),
#CTE_表4 AS (
    SELECT '表4' AS [表名], 时间戳, ROW_NUMBER() OVER (ORDER BY 唯一标识 DESC) AS RowNum
    FROM [4]
),
#CTE_表5 AS (
    SELECT '表5' AS [表名], 时间戳, ROW_NUMBER() OVER (ORDER BY 唯一标识 DESC) AS RowNum
    FROM [5]
)

SELECT [表名], [最后时间戳]
FROM (
    SELECT [表名], 时间戳 AS [最后时间戳], RowNum
    FROM #CTE_表1
    WHERE RowNum = 1

    UNION ALL

    SELECT [表名], 时间戳 AS [最后时间戳], RowNum
    FROM #CTE_表2
    WHERE RowNum = 1

    UNION ALL

    SELECT [表名], 时间戳 AS [最后时间戳], RowNum
    FROM #CTE_表3
    WHERE RowNum = 1

    UNION ALL

    SELECT [表名], 时间戳 AS [最后时间戳], RowNum
    FROM #CTE_表4
    WHERE RowNum = 1

    UNION ALL

    SELECT [表名], 时间戳 AS [最后时间戳], RowNum
    FROM #CTE_表5
    WHERE RowNum = 1


) AS [最后时间戳];

输出:

表名 最后时间戳
表1 2023-04-11 00:00:00.000
表2 2023-04-12 00:00:00.000
表3 2023-04-12 00:00:00.000
表4 2023-04-12 00:00:00.000
表5 2023-04-12 00:00:00.000

... 这个代码能够实现目标 - 但我对它不太满意:根据我对SQL的了解,这段代码似乎质量不高,也不容易阅读。

谢谢!

英文:

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

得分: 3

你可以使用 union all,并将表名作为字面值传递。

这有点棘手,因为在SQL Server中,我们不能直接在 union all 的成员中使用 order by,所以我们需要子查询。假设你的所有表都有 ts(时间戳)和 uniqueID(用于排序)列:

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)
英文:

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)

huangapple
  • 本文由 发表于 2023年6月8日 05:06:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定