编写一个选择查询,如果另一个“ id”具有相同的“ panum”,则显示“ id”

时间:2019-10-06 15:49:06

标签: sql sqlite

我需要回答的问题:

“显示在多篇论文上与另一位学者合作过的学者列表。在每行上列出每对学者对。仅列出他们的学术编号。不要列出重复的对。(例如,56,113和113,56是重复的对)”

到目前为止,iv一直在寻找一种方法来显示重复的“ panum”(纸张编号)查询,但我无法在线找到任何内容,说实话我不确定要搜索的内容

一些有用的信息:

Relation Schema:
ACADEMIC( acnum , deptnum*, famname, givename, initials, title)
AUTHOR( panum*, acnum* )
PAPER( panum , title)

Sample Data:
ACADEMIC values (221, 116, 'Korman', 'Robert', 'R J', 'Dr');
ACADEMIC values (222, 119, 'Morse', 'Frank', 'F J', 'Prof');
ACADEMIC values (223, 119, 'Bierling', 'Ken', 'K R E', 'Mr');
ACADEMIC values (224, 119, 'Ganguly', 'Ingo', 'I D R', 'Mr');
ACADEMIC values (225, 119, 'McAleer', 'David', 'D J G', null);

AUTHOR values (3001, 221);
AUTHOR values (3004, 222);
AUTHOR values (3001, 223);
AUTHOR values (3005, 224);
AUTHOR values (3004, 225);

PAPER values (3001, 'Speedup in parallel algorithms for adaptive quadrature');
PAPER values (3002, 'Odd-even reduction for banded linear equations');
PAPER values (3003, 'Estimating the mean of a correlated binary sequence');
PAPER values (3004, 'Optimality of the Fast Fourier transform');
PAPER values (3005, 'Time bounded random access machines with parallel processing');

如果我们使用示例数据作为示例。它应该输出在同一张纸上一起工作的人的口音。

1 个答案:

答案 0 :(得分:1)

您需要自连接表AUTHORGROUP BY来自表的每个副本中的一对学者,并在HAVING子句中设置条件:

select a1.acnum Academic1, a2.acnum Academic2
from author a1 inner join author a2
on a2.panum = a1.panum and a1.acnum < a2.acnum
group by a1.acnum, a2.acnum
having count(*) > 1