在查询中比较表与自身

时间:2015-04-10 20:46:30

标签: sql-server

我正在尝试运行一个SQL查询,它将提供重复记录的详细信息

查询是:

 USE DUBS
 SELECT a.Full_Name, a.status AS STAT1, b.status AS STAT2, a.PAID AS ID1, b.PAID AS ID2 
 from samples a, samples b 
 where a.Full_Name = b.Full_Name and a.status <>    b.status and a.PAID <> b.PAID and B.status <> a.status

但结果是:

 Full_Name           STAT1      STAT2         ID1                        ID2
 ----------------------------------------------------------------------------
 AAta01-OCT-1967    Active     Inactive       3100045433                40933
 AAta01-OCT-1967    Inactive    Active          40933                   3100045433

我喜欢他们就像这样

 Full_Name          STAT1   STAT2       ID1         ID2
 AAta01-OCT-1967    Active  Inactive    3100045433  40933

可以帮助我找不到的东西

1 个答案:

答案 0 :(得分:2)

您所拥有的是所谓的交叉连接或笛卡尔积的过滤版本。

假设您在samples中有两条记录。我们称他们为xy

您要加入samples给自己,所以如果您在samples中有两条记录,那么最终会有四条(未经过滤)的行对:

x joined with x
x joined with y
y joined with x
y joined with y

您正在过滤掉a的记录与b的记录相同的情况。所以你结束了这两对:

x joined with y
y joined with x

这解释了你所看到的。

为每个可能的配对获取一条记录的最简单,最快捷的方法?使用<运算符而不是<>作为ID:

SELECT a.Full_Name
     , a.status AS STAT1
     , b.status AS STAT2
     , a.PAID AS ID1
     , b.PAID AS ID2 
from   samples a, samples b 
where  a.Full_Name = b.Full_Name 
  and  a.status <> b.status 
  and  a.PAID < b.PAID 

这将进一步过滤您的记录,因此每个配对只能获得一条记录。