这是我试图回答的问题:
我拥有的是:
SELECT DISTINCT a1.acnum
FROM academic a1, academic a2
WHERE a1.deptnum = a2.deptnum
AND a1.acnum <> a2.acnum
AND a1.acnum IN (Select acnum
from interest
group by acnum
having count(acnum) >1);
这是错误的,因为我正在做的事情 如果acnum(学号)218与acnum 217在同一部门工作,AND与acnum 199(diff部门)具有相同的兴趣,那么我将acnum 218添加到列表中。
但是,我只应该添加第218和217页,如果他们两个都有相同数量的野外兴趣。
兴趣表有fieldnum,acnum学术表有acnum,deptnum,name
department table有deptnum,deptName
FIELDNUM ACNUM DESCRIP
------------------ --------------------
292 100 Multiprocessor and Special purpose computer design
293 100 General (HW)
293 197 Computer architecture
输出应该只列出所有学者的号码.. 但要明确一点:
Acnum Deptnum Interest
1 1 g&f
2 1 g&f
3 2 f
4 3 l
5 4 r&l
6 4 r&l
输出应为: 1 2 五 6
答案 0 :(得分:1)
未经测试但应该是好的
SELECT DISTINCT a1.acnum
FROM academic a1
INNER JOIN academic a2 ON a1.deptnum = a2.deptnum
AND
a1.acnum <> a2.acnum
INNER JOIN interest i1 ON a1.acnum=i1.acnum
GROUP BY a1.acnum
HAVING COUNT(i1.acnum)=(SELECT COUNT(*)
FROM interest i2
WHERE i1.acnum=i2.acnum)
答案 1 :(得分:0)
也许使用JOIN
可以在这里给你更好的结果:
SELECT DISTINCT a1.acnum
FROM academic a1
JOIN academic a2
ON a1.deptnum = a2.deptnum
AND a1.acnum <> a2.acnum
AND a1.acnum IN (Select acnum
from interest
group by acnum
having count(acnum) >1);
答案 2 :(得分:0)
根据我的理解,它应该有效
SELECT
listagg(a1.acnum,',') within group( order by a1.acnum) , a1.deptnum,a2.cnt
FROM
academic a1,
(Select
acnum,count(*) as cnt
from interest
group by acnum
) a2
where
a1.acnum=a2.acnum
group by
a1.deptnum,a2.cnt
having count(*)>1;
答案 3 :(得分:0)
使用公用表表达式(子查询)来获取学者,他们的部门以及他们的interestes计数。然后从中查询两次以获得所需的输出。
with cte as ( select a.acnum
, a.deptnum
, count(i.acnum) as int_cnt
from academic a
, interest i
where i.acnum = a.acnum
group by a.acnum
, a.deptnum
)
select ct1.acnum
, cte1.deptnum
, cte1.in_cnt
from cte cte1
, cte cte2
where cte2.deptnum = cte1.deptnum
and cte2.int_cnt = cte1.int_cnt
and cte2.acnum != cte1.acnum
order by cte1.deptnum
, cte1.acnum
警告 - 没有经过实际测试,所以虽然逻辑是合理的,但语法可能会出错;)