3

例如,我有 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 个表相同,因此被删除。我正在尝试使用左连接,但结果不正确。请提前帮助和感谢。我真的很感激。

4

5 回答 5

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      
于 2013-04-01T05:42:12.330 回答
1

没有时间对此进行测试,但这大约是正确的。不能在 SQLFiddle 中工作,因为 MySQL 不支持 except。

select * from (
  select * from have union select * from addon)
except
( select * from have, addon 
   where have.status=addon.status and have.name=addon.name)
于 2013-04-01T12:29:03.413 回答
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;
于 2013-04-01T13:37:22.300 回答
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 小提琴

于 2013-04-01T04:27:11.167 回答
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;
于 2013-04-02T11:25:12.603 回答