我有兩個數據表中包含數據類似如下: -TSQL鏈接數據分成匹配和不匹配的表
| id | name | dob | | name | dob |
|-------|------|----------| |------|----------|
| 12345 | ABC | 20010301 | | ABC | 20010301 | - matching record
| 45678 | DEF | 20010425 | | XYZ | 20010301 | - unmatched record
是否有可能寫這兩個表進行比較的查詢,然後創建一個匹配和一個不匹配的表,只留下orignal表結構/數據?
Match Table Unmatched Table
| id | rank | | id | rank |
|-------|------| |-------|------|
| 12345 | 1 | | 45678 | NULL |
我用MERGE嘗試,但我必須插入/更新一個源表,我已經打了我的天花板TSQL的方面 - 我也將處理超過3000萬行數據集 - 任何意見/建議?
sql(字段不匹配,但原則是存在的)我到目前爲止如下所示?
Create TABLE #Cohort ([ID] varchar(4),[match rank] int)
INSERT INTO #Cohort ([ID],[match rank]) VALUES('aaaa',NULL)
INSERT INTO #Cohort ([ID],[match rank]) VALUES('bbbb',NULL)
INSERT INTO #Cohort ([ID],[match rank]) VALUES('cccc',NULL)
INSERT INTO #Cohort ([ID],[match rank]) VALUES('dddd',NULL)
Create TABLE #link ([ID] varchar(4),[match rank] int)
INSERT INTO #link ([ID],[match rank]) VALUES(left(NEWID(),4),NULL)
INSERT INTO #link ([ID],[match rank]) VALUES(left(NEWID(),4),NULL)
INSERT INTO #link ([ID],[match rank]) VALUES('aaaa',NULL)
INSERT INTO #link ([ID],[match rank]) VALUES(left(NEWID(),4),NULL)
Create TABLE #Matches ([ID] varchar(4),[match rank] int)
Create TABLE #Unmatched ([ID] varchar(4),[match rank] int)
MERGE #Cohort tg
USING (SELECT distinct c.[ID], 1 as [match rank]
from #Cohort c
INNER JOIN #link as h on c.[ID]=h.[ID]) sc
ON (tg.[ID] = sc.[ID])
WHEN NOT MATCHED BY TARGET
THEN INSERT([ID],[match rank]) VALUES(sc.[ID],sc.[match rank])
WHEN NOT MATCHED BY SOURCE
THEN DELETE
OUTPUT Deleted.* INTO #Unmatched;