我正在解决有关SPJ数据库的一些问题。
S(SNUM, SNAME) [supplier]
P(PNUM, PNAME) [part]
J(JNUM, JNAME) [project]
SPJ(SNUM, PNUM, JNUM, Q)
查询是:获得至少有100个部分的项目对。
到目前为止,我已经为它编写了一个代码:
SELECT X.JNUM, Y.JNUM FROM SPJ AS X, SPJ AS Y
WHERE 100 <= (SELECT COUNT(PNUM) FROM (SELECT PNUM FROM SPJ WHERE SPJ.JN=X.JN
INTERSECT
SELECT PNUM FROM SPJ WHERE SPJ.JN=Y.JN));
我认为我的解决方案是正确的。有没有其他方法可以解决这个问题?
答案 0 :(得分:2)
只需获得所有相同的部分组合,然后查看每个项目对获得的匹配数:
select spj1.jnum as jnum1, spj2.jnum as jnum2
from spj spj1
join spj spj2 on spj1.pnum = spj2.pnum and spj1.jnum < spj2.jnum
group by spj1.jnum, spj2.jnum
having count(distinct spj1.pnum) >= 100;
答案 1 :(得分:0)
一些想法:
SPJ
之外,并且可能您不希望对角线相互镜像,即您不需要已为(SPJ1, SPJ2)
和(SPJ2, SPJ1)
返回了两对:
SELECT X.JNUM, Y.JNUM FROM SPJ AS X, SPJ AS Y
WHERE 100 <= (SELECT COUNT(PNUM)
FROM (SELECT PNUM FROM SPJ WHERE SPJ.JN=X.JN
INTERSECT
SELECT PNUM FROM SPJ WHERE SPJ.JN=Y.JN) as AnAlias)
AND X.PK > Y.PK; -- Avoid duplicates and self false positives