我需要将两个表与只有一列进行比较。 该查询应返回匹配项,而单独的查询应返回差异。
我创建了以下查询,但是仍然无法获取所需的信息。
两个表都只有一个名为IDNumber的列 列的架构信息完全相同 数据类型为Nvarchar 50
select * from EPE_Data_Cycle_77
Where [IDNumber] In ( select [IDNumber]
from [EPE_Data_Cycle_76])
表包含超过200万行 架构信息
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME ORDINAL_POSITION COLUMN_DEFAULT IS_NULLABLE DATA_TYPE CHARACTER_MAXIMUM_LENGTH CHARACTER_OCTET_LENGTH NUMERIC_PRECISION NUMERIC_PRECISION_RADIX NUMERIC_SCALE DATETIME_PRECISION CHARACTER_SET_CATALOG CHARACTER_SET_SCHEMA CHARACTER_SET_NAME COLLATION_CATALOG COLLATION_SCHEMA COLLATION_NAME DOMAIN_CATALOG DOMAIN_SCHEMA DOMAIN_NAME
XRG_P_SmartLife_Aug_2018 dbo EPE_Data_Cycle_76 IDNumber 1 NULL YES nvarchar 50 100 NULL NULL NULL NULL NULL NULL UNICODE NULL NULL Latin1_General_CI_AI NULL NULL NULL
XRG_P_SmartLife_Aug_2018 dbo EPE_Data_Cycle_77 IDNumber 1 NULL YES nvarchar 50 100 NULL NULL NULL NULL NULL NULL UNICODE NULL NULL Latin1_General_CI_AI NULL NULL NULL
答案 0 :(得分:0)
要执行此操作,并且不返回任何重复项,则需要在组合表上使用distinct。这是我完成任务的方式:
DECLARE @temp1 TABLE ( IDNumber NVARCHAR(MAX) )
DECLARE @temp2 TABLE ( IDNumber NVARCHAR(MAX) )
INSERT INTO @temp1 VALUES ('1'),('2'),('22'),('33')
INSERT INTO @temp2 VALUES ('3'),('2'),('77'),('33')
SELECT DISTINCT IDNumber FROM
(
SELECT * FROM @temp1
WHERE IDNumber IN (SELECT IDNumber FROM @temp2)
UNION
SELECT * FROM @temp2
WHERE IDNumber IN (SELECT IDNumber FROM @temp1)
) matches
SELECT DISTINCT IDNumber FROM
(
SELECT * FROM @temp1
WHERE IDNumber NOT IN (SELECT IDNumber FROM @temp2)
UNION
SELECT * FROM @temp2
WHERE IDNumber NOT IN (SELECT IDNumber FROM @temp1)
) differences
输出:
matches table:
2
33
differences table:
1
22
3
77