英文:
How to properly implement JOIN/ UNION ALL in a query when selecting columns from all tables in a database?
问题
I'm trying to select all tables with their max value of TimeLoaded
. TimeLoaded
is a timestamp. So my output should be similar to this:
我尝试选择所有表格及其TimeLoaded
的最大值。TimeLoaded
是一个时间戳。因此,我的输出应该类似于这样:
英文:
Assume I have many tables in a database.
I'm trying to select all tables with their max value of TimeLoaded
. TimeLoaded
is a timestamp. So my output should be similar to this:
Since I am trying to select every table in the database while retrieving the max value of TimeLoaded
, I am unsure how to implement JOIN and UNION ALL as there may be a database with 20 or more tables.
答案1
得分: 1
SELECT TOP (1) MAX(TimeLoaded) as TimeLoaded, 'Arrival' AS Tables FROM [your_db_name].[dbo].[Arrival]
Union
SELECT TOP (1) MAX(TimeLoaded) as TimeLoaded, 'Duration' AS Tables FROM [your_db_name].[dbo].[Duration]
Union
SELECT TOP (1) MAX(TimeLoaded) as TimeLoaded, 'Departure' AS Tables FROM [your_db_name].[dbo].[Departure]
Union
SELECT TOP (1) MAX(TimeLoaded) as TimeLoaded, 'Alert' AS Tables FROM [your_db_name].[dbo].[Alert]
英文:
I do not know how to select table name, but maybe you can try this:
SELECT TOP (1) MAX(TimeLoaded) as TimeLoaded, 'Arrival' AS Tables FROM [your_db_name].[dbo].[Arrival]
Union
SELECT TOP (1) MAX(TimeLoaded) as TimeLoaded, 'Duration' AS Tables FROM [your_db_name].[dbo].[Duration]
Union
SELECT TOP (1) MAX(TimeLoaded) as TimeLoaded, 'Departure' AS Tables FROM [your_db_name].[dbo].[Departure]
Union
SELECT TOP (1) MAX(TimeLoaded) as TimeLoaded, 'Alert' AS Tables FROM [your_db_name].[dbo].[Alert]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论