SQL Self Join没有双重交叉结果

时间:2011-09-16 21:22:42

标签: sql sqlite

我正在寻找解决方案,但是猫得到了它。

我的表:

ID         name          value1      value2
1          Meyer         20          _
2          Schmitt       20          _
3          Berger        _           20
4          Chief         _           20

我的疑问:

SELECT A.ID, A.name AS nameA, B.name AS nameB, A.value1 
FROM table AS A INNER JOIN table AS B
ON A.value1 = B.value2

结果:

ID      nameA      nameB       value1
1       Meyer      Berger      20
1       Meyer      Chief       20
2       Schmitt    Berger      20
2       Schmitt    Chief       20  

这应该是:

ID      nameA       nameB      value1
1       Meyer       Berger     20
2       Schmitt     Chief      20

如何获得此结果?

我也试过了GROUP BY A.name,但得到了错误的结果:

ID       nameA      nameB     value1
1        Meyer      Berger    20
2        Schmitt    Berger    20

3 个答案:

答案 0 :(得分:2)

根据这个加入标准A.value1 = B.value2和你的输入表,Meyer与Berger和Chief都有关,Schmitt与Berger和Chief都有关系。因此,对于您没有告诉我们这些相关的标准/业务规则,嫌疑人还有更多。 看起来你只是使用value1和value2作为一些时髦的外键。目前尚不清楚为什么你认为迈耶不会与酋长有关,或者为什么施密特不会与伯杰有关。

ID         name          value1      value2
1          Meyer         20          _
2          Schmitt       20          _
3          Berger        _           20
4          Chief         _           20

所以这不是您的查询的问题,但是您的数据结构存在问题,所以让我们修复value1和value2

ID         name          value1      value2
1          Meyer         10          _
2          Schmitt       20          _
3          Berger        _           10
4          Chief         _           20

现在您将获得正确的结果。但更传统的设计看起来像这样:

PersonId   PersonName    BossPersonId
1          Meyer         3
2          Schmitt       4
3          Berger        _
4          Chief         _

使用此查询:

Select A.Id, A.PersonName, B.PersonName as BossName
FROM table AS A INNER JOIN table AS B
ON A.BossPersonId= B.PersonId   

答案 1 :(得分:2)

您尚未指定DBMS。这是在SQL Server中测试的:

with C as
(
  select *,
         row_number() over(partition by value1 order by ID) as rn1,
         row_number() over(partition by value2 order by ID) as rn2
  from YourTable
)
select A.ID, 
       A.name as nameA, 
       B.name as nameB, 
       A.value1  
from C as A
  inner join C as B
    on A.value1 = B.value2 and
       A.rn1 = B.rn2

可测样品:

declare @T table
(
  ID int,
  name varchar(10),
  value1 int,
  value2 int
)
insert into @T values
(1,          'Meyer',         20,          null),
(2,          'Schmitt',       20,          null),
(3,          'Berger',        null,        20),
(4,          'Chief',         null,        20)

;with C as
(
  select *,
         row_number() over(partition by value1 order by ID) as rn1,
         row_number() over(partition by value2 order by ID) as rn2
  from @T
)
select A.ID, 
       A.name as nameA, 
       B.name as nameB, 
       A.value1  
from C as A
  inner join C as B
    on A.value1 = B.value2 and
       A.rn1 = B.rn2

答案 2 :(得分:0)

我现在解决了。

TABLE:

ID       name         value1         value2
1        Meyer        20             _
2        Muster       10             _
3        Heinz        20             _
4        Karl         _              20
5        Max          _              20
6        Zack         _              10

将其拆分为临时表并按值排序

表A:

ID       name        value1        value2
1        Muster      10            _
2        Meyer       20            _
3        Heinz       20            _

表B:

ID       name        value1        value2
1        Zack        _             10
2        Karl        _             20
3        Max         _             20

然后加入A.ID = B.ID AND A.value1 = B.value2

rownumber的帖子对我帮助很大,thx 最好的问候