为什么这不返回这些属性的唯一组合?

Why isn't this returning unique combinations of these attributes?

使用以下查询时:

with neededSkills(SkillCode) as (
    select distinct SkillCode
    from job natural join hasprofile natural join requires_skill
    where job_code = '1'
    minus
    select skillcode
    from person natural join hasskill
    where id = '1'
)
select distinct
  taughtin.c_code as c,
  count(taughtin.skillcode) as s,
  ti.c_code as cc,
  count(ti.skillcode) as ss
 from taughtin, taughtin ti
where taughtin.c_code <> ti.c_code
  and taughtin.skillcode <> ti.skillcode
  and taughtin.skillcode in (select skillcode from neededskills)
  and ti.skillcode in (select skillcode from neededskills)
group by (taughtin.c_code, ti.c_code)
order by (taughtin.c_code);

它returns:

 C  | S  | CC | SS
----|----|----|----
 1  | 1  | 2  | 1
 1  | 1  | 3  | 1
 1  | 1  | 5  | 1
 2  | 1  | 1  | 1
 3  | 1  | 1  | 1
 5  | 1  | 1  | 1

我希望它 return 仅在 CCC 的组合尚未使用的行。我是否误解了 group by 的工作原理?我将如何实现这个结果?

我正在尝试拥有它return:

 C  | S  | CC | SS
----|----|----|----
 1  | 1  | 2  | 1
 1  | 1  | 3  | 1
 1  | 1  | 5  | 1

我用Oracle SQLPlus.

您根据 taughtin.c_codeti.c_code 的组合进行分组,它们在查询上下文中是单独的列(即使它们在架构中是同一列)。一对1, 2和一对2, 1是不一样的;值可能相同,但来源不同。

如果您想以一种方式获得组合而不是另一种方式,那么最简单的事情就是始终使一个值大于另一个;而不是:

where taughtin.c_code <> ti.c_code

使用:

where ti.c_code > taughtin.c_code

尽管主查询也使用 ANSI 连接会更好,而且我不喜欢自然连接。你也不需要 distinct;第一个可能会消除重复项,但如果您仅使用 in()

的临时结果集,它们在逻辑上并不重要