1

我有一张像

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,即只有重复的科目

请指教,

4

8 回答 8

2
select * from table group by Subject having count(dept) > 1;
于 2013-05-07T11:38:44.860 回答
0

你可以试试这个:

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
于 2013-05-07T11:55:30.580 回答
0

尝试这个

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
于 2013-05-07T11:55:55.150 回答
0
 select * from tabelname where subject in ( select subject from tablename 
 group by subject having count(subject) > 1)                 
于 2013-05-07T11:41:53.417 回答
0

尝试以下:

select a.dept,a.Subject from tableName a,b where a.dept=b.dept
于 2013-05-07T11:37:12.593 回答
0

尝试以下查询:

SELECT dept, Subject FROM tableName WHERE Subject IN (SELECT Subject FROM tableName GROUP BY Subject HAVING COUNT(Subject)>1);
于 2013-05-07T11:41:29.060 回答
0
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查询。

于 2013-05-07T12:03:00.067 回答
0

将选项与 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上的演示

于 2013-05-07T14:38:48.667 回答