请参阅下面的DDL:
CREATE TABLE dbMatch1 (_Key_out int, reference int, dataset varchar(100), drivinglicense varchar(100), name varchar(100), dateofbirth datetime)
INSERT INTO dbMatch1 values (1,1,'Sales', '','Richard Williams','1980-11-07')
INSERT INTO dbMatch1 values (1,86,'Production', '','Richard Williams','1980-11-07')
INSERT INTO dbMatch1 values( 1,1008,'Finance', 'jdhdhdh8333','Richard Williams','1980-11-07')
INSERT INTO dbMatch1 values (2,90,'Marketing', '','Dick Williams','1980-11-07')
INSERT INTO dbMatch1 values (3,251,'Sales', '','Ian McWilliams','1971-02-02')
CREATE TABLE dbMatch2 (_Key_out int, reference int, dataset varchar(100), DrivingLicense varchar(100),name varchar(100), dateofbirth datetime)
INSERT INTO dbMatch2 values (100,1008,'Finance', 'jdhdhdh8333','Richard Williams','1980-11-07')
INSERT INTO dbMatch2 values (100,90,'Marketing', 'jdhdhdh8333','','')
INSERT INTO dbMatch2 values (100,95,'Sales', 'jdhdhdh8333','','')
INSERT INTO dbMatch2 values (101,45,'Production', 'jlbfjldfnl','','')
INSERT INTO dbMatch2 values (102,455,'Marketing', 'jlbfjldfnl','','')
_key_out列标识重复的记录。销售记录1,生产记录86和财务记录1008与dbMatch1和财务记录1008重复,营销记录90和销售记录95与dbMatch 2重复。
如何合并这些结果?例如,我希望dbMatch1中的前三个记录和dbMatch2中的前两个记录属于同一组。
我试过这个:
从dbmatch1中选择* 联盟 从dbmatch2中选择*
问题是以下行的_key_out ids不相同:
1 1 Sales Richard Williams 1980-11-07 00:00:00.000
1 86 Production Richard Williams 1980-11-07 00:00:00.000
1 1008 Finance jdhdhdh8333 Richard Williams 1980-11-07 00:00:00.000
100 90 Marketing jdhdhdh8333 1900-01-01 00:00:00.000
100 95 Sales jdhdhdh8333 1900-01-01 00:00:00.000
100 1008 Finance jdhdhdh8333 Richard Williams 1980-11-07 00:00:00.000
答案 0 :(得分:0)
我不确定您尝试了什么但是不能使用UNION
合并结果
(select * from dbmatch1) -- giving first part duplicates
UNION
(select * from dbmatch2) -- giving second part duplicates