我在oracle中有一个具有非唯一列值的表。这些组合也是非独特的。但是,特定顺序的关联必须是唯一的。我尝试了很多解决方案。 This 问题是最接近的,但我需要Oracle SQL中的解决方案。以下是我的表
--------------------------------------------------
Teacher subject class_id
--------------------------------------------------
Paul English 001
Paul English 002
Allen English 003
Sia Maths 134
John Computer 913
Jack Physics 341
Arlene Maths 001
-------------------------------------------------
查询应仅返回以下信息
English, Maths
即与多位教师相关的科目。
答案 0 :(得分:1)
也许你想要这样的东西:
select listagg (subject, ', ') within group (order by subject) subjects
from (
select subject from classes
group by subject
having count(teacher)>1
);
SUBJECTS
-------------------------
English, Maths
或者您可以使用分析函数获得相同的结果:
select listagg (subject, ', ') within group (order by subject) subjects
from (
select subject,
count(teacher) over (partition by subject) teachers,
row_number() over (partition by subject order by class_id) rn
from classes
)
where teachers>1 and rn=1
;
答案 1 :(得分:0)
如果我理解正确,您希望拥有多位教师的科目。这是一个带有having
子句的简单聚合查询:
select subject
from t
group by subject
having min(teacher) <> max(teacher);