例如,我有2个这样的表
data have;
input name $ status $;
datalines;
A a
B b
C c
;;;;
run;
第二桌:
data addon;
input name $ status $;
datalines;
A a
C f
D d
E e
F f
B z
;;;;
run;
如何获得如下结果:
B b
C c
C f
D d
E e
F f
B z
A-a行与2个表相同,因此被删除。我正在尝试使用左连接,但结果不对。请提前帮助和感谢。我真的很感激。
答案 0 :(得分:1)
试试这个:
SELECT COALESCE(table1.input, table2.input) AS input
, COALESCE(table1.status, table2.status) AS status
FROM table1
FULL OUTER JOIN table2 ON table1.input = table2.input
AND table1.status = table2.status
WHERE (table1.input IS NULL OR table2.input IS NULL)
ORDER BY 1
输出
INPUT STATUS
----- ------
B b
B z
C f
C c
D d
E e
F f
答案 1 :(得分:1)
没有时间对此进行测试,但这是大致正确的。由于MySQL不支持除了。
,因此无法在SQLFiddle中工作select * from (
select * from have union select * from addon)
except
( select * from have, addon
where have.status=addon.status and have.name=addon.name)
答案 2 :(得分:1)
另一种方式
data have;
input name $ status $;
datalines;
A a
B b
C c
;;;;
run;
data addon;
input name $ status $;
datalines;
A a
C f
D d
E e
F f
B z
;;;;
run;
Data Together;
Set have addon;
/* If the data sets were already sorted */
/* By Name Status; */
/* Then skip the Proc Sort */
Run;
Proc sort data=together;
by name status;
Run;
Data final;
Set Together;
by name status;
if first.status and last.status;
Run;
答案 3 :(得分:0)
SELECT t1.name,t1.status
FROM
(
SELECT name,status
FROM have
UNION ALL
SELECT name,status
FROM addon
) as t1
JOIN have t2 ON t1.name!=t2.name AND t1.status!=t2.status
JOIN addon t3 ON t2.name=t3.name AND t2.status=t3.status
我为你创建了SQL fiddle。
答案 4 :(得分:0)
data have;
input name $ status $;
datalines;
A a
B b
C c
;;;;
run;
第二桌:
data addon;
input name $ status $;
datalines;
A a
C f
D d
E e
F f
B z
;;;;
run;
如何获得如下结果:
B b
C c
C f
D d
E e
F f
B z
简单使用合并声明。在此步骤之前使用键对数据集进行排序
DATA RESULT;
KEEP H.NAME A.STATUS;
MERGE HAVE(IN = H) ADDON (IN = A);
BY NAME;
RUN;