SQL在同一个表中比较COUNT值

时间:2013-02-03 01:41:57

标签: sql oracle

我试图解决一个看似简单的问题,但我想我已经开始了解EXISTS关键字的工作原理。问题很简单(这是实际问题的一个愚蠢版本) - 我有一张学生表和一张爱好表。学生表有他们的学生证和姓名。只返回拥有相同数量爱好的学生(即那些拥有独特爱好数量的学生将不会出现)

所以我遇到的困难是如何比较爱好的数量。我试过的就是这个。

SELECT sa.studentnum, COUNT(ha.hobbynum)
FROM student sa, hobby ha
WHERE sa.studentnum = ha.studentnum
AND EXISTS (SELECT *
            FROM student sb, hobby hb
            WHERE sb.studentnum = hb.studentnum 
            AND sa.studentnum != sb.studentnum
            HAVING COUNT(ha.hobbynum) = COUNT(hb.hobbynum)
           )
GROUP BY sa.studentnum
ORDER BY sa.studentnum;

所以似乎正在发生的事情是每个测试的爱好数量是相同的,导致返回所有原始表,而不是那些匹配相同数量的爱好的表。

2 个答案:

答案 0 :(得分:2)

未经测试,但可能是这样的(如果我正确理解问题):

WITH h AS (
  SELECT studentnum, COUNT(hobbynum) OVER (PARTITION BY studentnum) student_hobby_ct
    FROM hobby)
SELECT studentnum, student_hobby_ct
  FROM h h1 JOIN h h2 ON h1.student_hobby_ct = h2.student_hobby_ct AND
                         h1.studentnum <> h2.studentnum;

答案 1 :(得分:1)

我认为您的查询所做的只是让至少有一名其他学生拥有相同数量的爱好的学生返回。但是你并没有回复与他们相匹配的学生。这是故意的吗?我将这两个查询视为子查询,并在计数加入之前进行聚合。你可以做几件事......在这里它返回了具有匹配的爱好数量的学生数量,但你可以限制HAVING(COUNT(distinct sb.studentnum)= 0来获得你的查询似乎返回的结果。 ..

with xx as 
(SELECT sa.studentnum, count(ha.hobbynum) hobbycount
            FROM student sa inner join hobby ha
            on sa.studentnum = ha.studentnum
            group by sa.studentnum
           )
select sa.studentnum, sa.hobbycount, count(distinct sb.studentnum) as matchcount
from
xx sa inner join xx sb on 
    sa.hobbycount = sb.hobbycount
where
    sa.studentnum != sb.studentnum
GROUP by sa.studentnum, sa.hobbycount
ORDER BY sa.studentnum;