我有一张像
DEPT Years Subject
ECE 1 Maths
ECE 1 PDC
EEE 1 Maths
EEE 1 ET
MEC 1 Maths
MEC 1 ET
我希望查询找到所有重复科目的部门(部门),如 ECE 中的数学、EEE 和 MEC 以及在 EEE 和 MEC 中重复的 ET,即只有重复的科目
请指教,
我有一张像
DEPT Years Subject
ECE 1 Maths
ECE 1 PDC
EEE 1 Maths
EEE 1 ET
MEC 1 Maths
MEC 1 ET
我希望查询找到所有重复科目的部门(部门),如 ECE 中的数学、EEE 和 MEC 以及在 EEE 和 MEC 中重复的 ET,即只有重复的科目
请指教,
select * from table group by Subject having count(dept) > 1;
你可以试试这个:
SELECT DEPT, Subject FROM Table1 WHERE Subject IN
(SELECT Subject FROM Table1 GROUP BY Subject HAVING COUNT(Subject)>1);
现场示例:
http://sqlfiddle.com/#!2/b5885/12
输出如下:
DEPT SUBJECT
ECE Maths
EEE Maths
EEE ET
MEC Maths
MEC ET
尝试这个
SELECT * FROM XYZ WHERE SUBJECT IN
(SELECT SUBJECT FROM XYZ HAVING COUNT(SUBJECT)>1
GROUP BY SUBJECT);
这是结束表
DEPT YEAR SUBJECT
---------- ---------- ----------
EEE 1 MATHS
EEE 1 ET
MEC 1 MATHS
MEC 1 ET
select * from tabelname where subject in ( select subject from tablename
group by subject having count(subject) > 1)
尝试以下:
select a.dept,a.Subject from tableName a,b where a.dept=b.dept
尝试以下查询:
SELECT dept, Subject FROM tableName WHERE Subject IN (SELECT Subject FROM tableName GROUP BY Subject HAVING COUNT(Subject)>1);
SELECT
DEPT,
Subject
FROM tablename
WHERE
Subject IN (
SELECT [Subject] FROM (
SELECT [Subject],count([Subject]) AS CountSubj FROM tablename GROUP BY [Subject]
) AS subtbl
WHERE subtbl.CountSubj>1)
ORDER BY Subject
试试上面的sql查询。
将选项与 EXISTS 运算符一起使用
SELECT *
FROM dbo.test110 t
WHERE EXISTS (
SELECT 1
FROM dbo.test110 t2
WHERE t.Subject = t2.Subject
HAVING COUNT(t2.Subject) > 1
)
SQLFiddle上的演示