SQL 查询表之间的变化汇总
SQL query for summary of change between tables
有两个 table EmployeeA
和 EmployeeB
包含员工信息。两者都具有相同的列,例如 Guid、FullName、Age、Salary 和 Address。 table 都有很多记录,每个员工在每个 table.
中最多有 1 条记录
EmployeeA
有点原始 table 并且数据无法更改。所以它是只读的,其中 EmployeeA
中的数据是从 API.
导入的
最初EmployeeB
table中的数据来自EmployeeA
table到UI,用户可以从UI修改它。所以EmployeeB
可以修改数据。
我只需要在 EmployeeB
table.
中修改的那些列的更改结果
示例:
Column Name EmployeeA's Data EMployeeB's Data
--------------------------------------------------
Salary 000 0000
Address ABC, USA PQR, USA
(全名和年龄栏没有变化).
return 上述结果集的查询是什么样的?
没有示例数据,但可能类似于:
WITH
A AS
(
SELECT 1 "ID", 'NAME 1' "NAME", 100 "SALARY", 'ABC' "ADDRESS", 24 "AGE" FROM DUAL UNION ALL
SELECT 2 "ID", 'NAME 2' "NAME", 200 "SALARY", 'DEF' "ADDRESS", 30 "AGE" FROM DUAL UNION ALL
SELECT 3 "ID", 'NAME 3' "NAME", 300 "SALARY", 'GEH' "ADDRESS", 32 "AGE" FROM DUAL
),
B AS
(
SELECT 1 "ID", 'NAME 1' "NAME", 100 "SALARY", 'ABC' "ADDRESS", 24 "AGE" FROM DUAL UNION ALL
SELECT 2 "ID", 'NAME 2' "NAME", 220 "SALARY", 'DEF, DEF' "ADDRESS", 30 "AGE" FROM DUAL UNION ALL
SELECT 3 "ID", 'NAME 3' "NAME", 330 "SALARY", 'GEH' "ADDRESS", 32 "AGE" FROM DUAL
)
SELECT
A.NAME "NAME", 'SALARY' "COLUMN_NAME", To_Char(A.SALARY) "A_DATA", To_Char(B.SALARY) "B_DATA"
FROM
A
INNER JOIN
B ON(A.ID = B.ID)
WHERE
A.SALARY <> B.SALARY
UNION ALL
SELECT
A.NAME "NAME", 'ADDRESS' "COLUMN_NAME", A.ADDRESS "A_DATA", B.ADDRESS "B_DATA"
FROM
A
INNER JOIN
B ON(A.ID = B.ID)
WHERE
A.ADDRESS <> B.ADDRESS
ORDER BY 1
--
-- Result
-- NAME COLUMN A_DATA B_DATA
-- NAME 2 SALARY 200 220
-- NAME 2 ADDRESS DEF DEF, DEF
-- NAME 3 SALARY 300 330
加入两个 table 并比较两个 table 的值。使用 APPLY
旋转它
select a.GUID, c.ColumnName, c.EmpA, c.EmpB
from EmployeeA a
inner join EmployeeB b on a.GUID = b.GUID
cross apply
(
select ColumnName = 'FullName',
EmpA = a.FullName,
EmpB = b.FullName
where a.FullName <> b.FullName
union all
select ColumnName = 'Age',
EmpA = convert(varchar(100), a.Age),
EmpB = convert(varchar(100), b.Age)
where a.Age <> b.Age
union all
select ColumnName = 'Salary',
EmpA = convert(varchar(100), a.Salary),
EmpB = convert(varchar(100), b.Salary)
where a.Salary <> b.Salary
union all
select ColumnName = 'Address',
EmpA = a.Address, EmpB = b.Address
where a.Address <> b.Address
) c
注意:您可能希望更改上述查询以处理空值
有两个 table EmployeeA
和 EmployeeB
包含员工信息。两者都具有相同的列,例如 Guid、FullName、Age、Salary 和 Address。 table 都有很多记录,每个员工在每个 table.
EmployeeA
有点原始 table 并且数据无法更改。所以它是只读的,其中 EmployeeA
中的数据是从 API.
最初EmployeeB
table中的数据来自EmployeeA
table到UI,用户可以从UI修改它。所以EmployeeB
可以修改数据。
我只需要在 EmployeeB
table.
示例:
Column Name EmployeeA's Data EMployeeB's Data
--------------------------------------------------
Salary 000 0000
Address ABC, USA PQR, USA
(全名和年龄栏没有变化).
return 上述结果集的查询是什么样的?
没有示例数据,但可能类似于:
WITH
A AS
(
SELECT 1 "ID", 'NAME 1' "NAME", 100 "SALARY", 'ABC' "ADDRESS", 24 "AGE" FROM DUAL UNION ALL
SELECT 2 "ID", 'NAME 2' "NAME", 200 "SALARY", 'DEF' "ADDRESS", 30 "AGE" FROM DUAL UNION ALL
SELECT 3 "ID", 'NAME 3' "NAME", 300 "SALARY", 'GEH' "ADDRESS", 32 "AGE" FROM DUAL
),
B AS
(
SELECT 1 "ID", 'NAME 1' "NAME", 100 "SALARY", 'ABC' "ADDRESS", 24 "AGE" FROM DUAL UNION ALL
SELECT 2 "ID", 'NAME 2' "NAME", 220 "SALARY", 'DEF, DEF' "ADDRESS", 30 "AGE" FROM DUAL UNION ALL
SELECT 3 "ID", 'NAME 3' "NAME", 330 "SALARY", 'GEH' "ADDRESS", 32 "AGE" FROM DUAL
)
SELECT
A.NAME "NAME", 'SALARY' "COLUMN_NAME", To_Char(A.SALARY) "A_DATA", To_Char(B.SALARY) "B_DATA"
FROM
A
INNER JOIN
B ON(A.ID = B.ID)
WHERE
A.SALARY <> B.SALARY
UNION ALL
SELECT
A.NAME "NAME", 'ADDRESS' "COLUMN_NAME", A.ADDRESS "A_DATA", B.ADDRESS "B_DATA"
FROM
A
INNER JOIN
B ON(A.ID = B.ID)
WHERE
A.ADDRESS <> B.ADDRESS
ORDER BY 1
--
-- Result
-- NAME COLUMN A_DATA B_DATA
-- NAME 2 SALARY 200 220
-- NAME 2 ADDRESS DEF DEF, DEF
-- NAME 3 SALARY 300 330
加入两个 table 并比较两个 table 的值。使用 APPLY
旋转它
select a.GUID, c.ColumnName, c.EmpA, c.EmpB
from EmployeeA a
inner join EmployeeB b on a.GUID = b.GUID
cross apply
(
select ColumnName = 'FullName',
EmpA = a.FullName,
EmpB = b.FullName
where a.FullName <> b.FullName
union all
select ColumnName = 'Age',
EmpA = convert(varchar(100), a.Age),
EmpB = convert(varchar(100), b.Age)
where a.Age <> b.Age
union all
select ColumnName = 'Salary',
EmpA = convert(varchar(100), a.Salary),
EmpB = convert(varchar(100), b.Salary)
where a.Salary <> b.Salary
union all
select ColumnName = 'Address',
EmpA = a.Address, EmpB = b.Address
where a.Address <> b.Address
) c
注意:您可能希望更改上述查询以处理空值