将来自不同表的数据与 Table 名称一起合并到同一 SQL 数据库中
Combine Data from Different tables in same SQL Database along with the Table Name
我正在尝试从同一个 SQL 数据库中的多个 Table 中获取数据以及获取这些数据的 Table 名称。
Select [timestamp]
,[User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
FROM
(SELECT * FROM [LIVE].[dbo].[LIVE Database1$User Setup]
Union ALL
SELECT * FROM [LIVE].[dbo].[LIVE_Database2$User Setup]
Union ALL
SELECT * FROM [LIVE].[dbo].[LIVE_Database3$User Setup]) data
GROUP BY
[timestamp]
,[User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
HAVING count(*)!=2
For value like below
Select [User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
, Table_Name
FROM
(
Select [User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
,'Table1' AS Table_Name
FROM [DB].[dbo].[UAT Table1$User Setup] as Tablename1
Union ALL
Select [User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
,'Table2' AS Table_Name
FROM [DB].[dbo].[UAT Table2$User Setup]as Tablename1
Union ALL
Select [User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
,'Table3' AS Table_Name
FROM [DB].[dbo].[Table3$User Setup]as Tablename1) data
GROUP BY
[User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
,Table_Name
HAVING count(*)!=2
我正在尝试从同一个 SQL 数据库中的多个 Table 中获取数据以及获取这些数据的 Table 名称。
Select [timestamp]
,[User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
FROM
(SELECT * FROM [LIVE].[dbo].[LIVE Database1$User Setup]
Union ALL
SELECT * FROM [LIVE].[dbo].[LIVE_Database2$User Setup]
Union ALL
SELECT * FROM [LIVE].[dbo].[LIVE_Database3$User Setup]) data
GROUP BY
[timestamp]
,[User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
HAVING count(*)!=2
For value like below
Select [User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
, Table_Name
FROM
(
Select [User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
,'Table1' AS Table_Name
FROM [DB].[dbo].[UAT Table1$User Setup] as Tablename1
Union ALL
Select [User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
,'Table2' AS Table_Name
FROM [DB].[dbo].[UAT Table2$User Setup]as Tablename1
Union ALL
Select [User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
,'Table3' AS Table_Name
FROM [DB].[dbo].[Table3$User Setup]as Tablename1) data
GROUP BY
[User ID]
,[HCA Location Code]
,[HCA Lead]
,[PC Location Code]
,[Approval Administrator]
,[Time Sheet Admin_]
,Table_Name
HAVING count(*)!=2