SQL 交叉连接中的重复对?
Duplicate pairs in SQL cross join?
我知道 SO 上有很多类似的问题,我查看了其中的一些,即 this,但它不足以解决我的问题
我有一个 table proposals
看起来像这样:
proposalNum | PI | department | investigator | investigatorDepartment|
----------------------------------------------------------------------------
FP00003521 | Bush,Raj | GIS | Amm,Anna | CIS |
FP00003521 | Bush,Raj | GIS | Milton,Ross | CIS |
FP00003521 | Bush,Raj | GIS | Landis, Amy | SEB |
FP00069606 | Mill, Ash | DIA | Keller, Bill | FAA |
我基本上想查看 investigator
字段并创建:
Amm,Anna | Milton, Ross
Amm,Anna | Landis, Amy
Milton,Ross | Landis, Amy
所有 CIS
的调查员,以及给定的 proposalNum
(在本例中,FP00003521
)
(这里是SQL FIDDLE)
我这样做了:
SELECT p1.investigator,p2.investigator
FROM proposals AS p1
CROSS JOIN proposals AS p2
WHERE p1.investigatorDepartment='CIS'
AND p1.investigator!=p2.investigator
AND p1.proposalNum=p2.proposalNum
并得到:
Milton,Ross | Amm,Anna
Amm,Anna | Milton,Ross
Amm,Anna | Landis, Amy
Milton,Ross | Landis,Amy
但是那里有重复...
按照链接post上的逻辑,我也试过:
SELECT p1.investigator,p2.investigator
FROM proposals AS p1
CROSS JOIN proposals AS p2
ON p1.investigator < p2.investigator
WHERE p1.investigatorDepartment='CIS'
AND p1.investigator!=p2.investigator
AND p1.proposalNum=p2.proposalNum
我得到了这个:
Amm,Anna | Milton,Ross
Amm,Anna | Landis, Amy
问题是当我添加 ON p1.investigator < p2.investigator
条件时,它也不会添加 Milton,Ross
和 Landis, Amy
,因为 M > L
我将如何重写我的查询来解决这个问题?
任何帮助将不胜感激,谢谢!!
您可以加入 p2's
独联体和非独联体的不同方式。一种方式如下。
select
p1.investigator,
p2.investigator
from
proposals AS p1
cross join
proposals AS p2
where
p1.investigatorDepartment = 'CIS' and ((
p2.investigatorDepartment = 'CIS' and
p1.investigator < p2.investigator
) or (
p2.investigatorDepartment != 'CIS' or
p2.investigatorDepartment is null
)) and
p1.proposalNum = p2.proposalNum;
我知道 SO 上有很多类似的问题,我查看了其中的一些,即 this,但它不足以解决我的问题
我有一个 table proposals
看起来像这样:
proposalNum | PI | department | investigator | investigatorDepartment|
----------------------------------------------------------------------------
FP00003521 | Bush,Raj | GIS | Amm,Anna | CIS |
FP00003521 | Bush,Raj | GIS | Milton,Ross | CIS |
FP00003521 | Bush,Raj | GIS | Landis, Amy | SEB |
FP00069606 | Mill, Ash | DIA | Keller, Bill | FAA |
我基本上想查看 investigator
字段并创建:
Amm,Anna | Milton, Ross
Amm,Anna | Landis, Amy
Milton,Ross | Landis, Amy
所有 CIS
的调查员,以及给定的 proposalNum
(在本例中,FP00003521
)
(这里是SQL FIDDLE)
我这样做了:
SELECT p1.investigator,p2.investigator
FROM proposals AS p1
CROSS JOIN proposals AS p2
WHERE p1.investigatorDepartment='CIS'
AND p1.investigator!=p2.investigator
AND p1.proposalNum=p2.proposalNum
并得到:
Milton,Ross | Amm,Anna
Amm,Anna | Milton,Ross
Amm,Anna | Landis, Amy
Milton,Ross | Landis,Amy
但是那里有重复...
按照链接post上的逻辑,我也试过:
SELECT p1.investigator,p2.investigator
FROM proposals AS p1
CROSS JOIN proposals AS p2
ON p1.investigator < p2.investigator
WHERE p1.investigatorDepartment='CIS'
AND p1.investigator!=p2.investigator
AND p1.proposalNum=p2.proposalNum
我得到了这个:
Amm,Anna | Milton,Ross
Amm,Anna | Landis, Amy
问题是当我添加 ON p1.investigator < p2.investigator
条件时,它也不会添加 Milton,Ross
和 Landis, Amy
,因为 M > L
我将如何重写我的查询来解决这个问题?
任何帮助将不胜感激,谢谢!!
您可以加入 p2's
独联体和非独联体的不同方式。一种方式如下。
select
p1.investigator,
p2.investigator
from
proposals AS p1
cross join
proposals AS p2
where
p1.investigatorDepartment = 'CIS' and ((
p2.investigatorDepartment = 'CIS' and
p1.investigator < p2.investigator
) or (
p2.investigatorDepartment != 'CIS' or
p2.investigatorDepartment is null
)) and
p1.proposalNum = p2.proposalNum;