需要SQL查询

时间:2013-05-07 11:34:21

标签: sql

我有一张像

这样的表格
DEPT   Years   Subject
ECE      1       Maths
ECE      1       PDC
EEE      1       Maths
EEE      1       ET
MEC      1       Maths
MEC      1       ET

我希望查询找到所有重复主题的DEPT(部门),如ECE,EEE和EEE中的数学。在EEE和EEE中重复使用MEC和ET MEC,即只重复一次

请建议,

8 个答案:

答案 0 :(得分:2)

select * from table group by Subject having count(dept) > 1;

答案 1 :(得分:0)

试试以下内容:

select a.dept,a.Subject from tableName a,b where a.dept=b.dept

答案 2 :(得分:0)

尝试以下查询:

SELECT dept, Subject FROM tableName WHERE Subject IN (SELECT Subject FROM tableName GROUP BY Subject HAVING COUNT(Subject)>1);

答案 3 :(得分:0)

 select * from tabelname where subject in ( select subject from tablename 
 group by subject having count(subject) > 1)                 

答案 4 :(得分: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

答案 5 :(得分: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

答案 6 :(得分: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查询。

答案 7 :(得分: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上的演示