就是做类似A UNION B的两表UNION操作,当A B两表的编号相同时,取A表信息,求个参考SQL
select * from A
union all
select * from B where B.ID not in (select ID from A)
--已解决
WITH S1 AS(
SELECT , 'A' AS Flag FROM Supply
), S2 AS(
SELECT , 'B' AS Flag FROM supplyCopy
), S3 AS(
SELECT * FROM S1 UNION ALL
SELECT * FROM S2
)
SELECT p.
FROM (
SELECT
, ROW_NUMBER() OVER (PARTITION BY S3.id ORDER BY S3.Flag ASC) AS rn
FROM S3
) p
WHERE p.rn = 1
-------------------------
WITH S1 AS(
SELECT , 'A' AS Flag FROM Supply
), S2 AS(
SELECT , 'B' AS Flag FROM supplyCopy
), S3 AS(
SELECT * FROM S1 UNION ALL
SELECT * FROM S2
), S4 AS(
SELECT SUPPLYID, MIN(FLAG) AS minFlag FROM S3 GROUP BY supplyId
)
SELECT
S3.* FROM S3 JOIN S4
ON
S3.supplyId = S4.supplyId AND S3.Flag = S4.minFlag