3

这是我试图回答的问题:

我所拥有的是:

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 在同一部门工作,并且与 acnum 199(差异部门)具有相同的兴趣,那么我将 acnum 218 添加到列表中。

但是,如果他们都有相同数量的领域兴趣,我应该只添加 acnum 218 和 217。

兴趣表有 fieldnum , acnum

学术表有 acnum 、 deptnum 、 name

部门表有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 5 6

4

4 回答 4

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)
于 2013-02-11T07:41:00.470 回答
0

使用公用表表达式(子查询)来获取学者、他们的部门和他们的兴趣计数。然后从中查询两次以获得所需的输出。

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

警告- 未经实际测试,因此虽然逻辑合理,但语法可能有误;)

于 2013-02-11T09:11:36.260 回答
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);
于 2013-02-11T07:26:45.110 回答
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;
于 2013-02-11T07:44:31.903 回答