1
我試圖根據兩個標準獲取兩個數據庫之間的唯一記錄。的標準是:TSQL UNION獲取獨特價值
- 如果數據在數據庫1實測值(@SCCM下面的示例所示),它 被給予優先
- 抓鬥選定的數據庫內的MAX資源ID
這是一個例子,這是一半工作。數據庫首選項正在工作,但該數據庫中的最大資源ID不在。現在它選擇既@SMS和@SCCM
DECLARE @SMS TABLE (
name0 varchar(100),
resid int
)
DECLARE @SCCM TABLE (
name0 varchar(100),
resid int
)
INSERT INTO @SMS
SELECT 'TEST', 1000 UNION
SELECT 'TEST', 1500 UNION
SELECT 'TEST1', 2000 UNION
SELECT 'TEST2', 3000 UNION
SELECT 'TEST3', 4000
INSERT INTO @SCCM
SELECT 'TEST', 100 UNION
SELECT 'TEST', 150 UNION
SELECT 'TEST1', 200 UNION
SELECT 'TEST2', 300
SELECT MIN(SMSDB) as SMSDB, MAX(Resid), Name0 FROM
(
SELECT name0, resid, 2 as SMSDB FROM @SMS
UNION ALL
SELECT name0, resid, 1 as SMSDB FROM @SCCM
) as tbl
GROUP BY NAME0
預期結果之間的最大:
SMSDB | Resid | Name0
----------------------
1 | 150 | TEST
1 | 200 | TEST1
1 | 300 | TEST2
2 | 4000 | TEST3