For example, I have 2 tables like this
例如,我有两个这样的表。
data have;
input name $ status $;
datalines;
A a
B b
C c
;;;;
run;
2nd table:
表2:
data addon;
input name $ status $;
datalines;
A a
C f
D d
E e
F f
B z
;;;;
run;
How do I get the result like below:
如何得到如下结果:
B b
C c
C f
D d
E e
F f
B z
The row A - a is the same from 2 tables so it got removed. I'm trying to use left join but the result is not right. Please help and thanks in advance. I'm really appreciated it.
A - A的行从两个表中是一样的,所以它被删除了。我试着用左连接,但结果是不对的。请提前帮助和感谢。我很欣赏它。
5 个解决方案
#1
1
Another way
另一种方式
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;
#2
1
Try this:
试试这个:
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
Output :
输出:
INPUT STATUS
----- ------
B b
B z
C f
C c
D d
E e
F f
#3
1
Don't have time to test this, but this is approximately right. Won't work in SQLFiddle since MySQL doesn't support except.
没有时间进行测试,但这是大致正确的。不会在SQLFiddle工作,因为MySQL不支持。
select * from (
select * from have union select * from addon)
except
( select * from have, addon
where have.status=addon.status and have.name=addon.name)
#4
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
I created an SQL fiddle for you.
我为你创建了一个SQL小提琴。
#5
0
data have;
input name $ status $;
datalines;
A a
B b
C c
;;;;
run;
2nd table:
表2:
data addon;
input name $ status $;
datalines;
A a
C f
D d
E e
F f
B z
;;;;
run;
How do I get the result like below:
如何得到如下结果:
B b
C c
C f
D d
E e
F f
B z
Simple Use merge statement. Sort the datasets using the keys before this step
简单使用merge语句。在此步骤之前,使用键对数据集进行排序
DATA RESULT;
KEEP H.NAME A.STATUS;
MERGE HAVE(IN = H) ADDON (IN = A);
BY NAME;
RUN;
#1
1
Another way
另一种方式
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;
#2
1
Try this:
试试这个:
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
Output :
输出:
INPUT STATUS
----- ------
B b
B z
C f
C c
D d
E e
F f
#3
1
Don't have time to test this, but this is approximately right. Won't work in SQLFiddle since MySQL doesn't support except.
没有时间进行测试,但这是大致正确的。不会在SQLFiddle工作,因为MySQL不支持。
select * from (
select * from have union select * from addon)
except
( select * from have, addon
where have.status=addon.status and have.name=addon.name)
#4
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
I created an SQL fiddle for you.
我为你创建了一个SQL小提琴。
#5
0
data have;
input name $ status $;
datalines;
A a
B b
C c
;;;;
run;
2nd table:
表2:
data addon;
input name $ status $;
datalines;
A a
C f
D d
E e
F f
B z
;;;;
run;
How do I get the result like below:
如何得到如下结果:
B b
C c
C f
D d
E e
F f
B z
Simple Use merge statement. Sort the datasets using the keys before this step
简单使用merge语句。在此步骤之前,使用键对数据集进行排序
DATA RESULT;
KEEP H.NAME A.STATUS;
MERGE HAVE(IN = H) ADDON (IN = A);
BY NAME;
RUN;