如何在同一个表中组合多个查询

时间:2012-12-29 06:44:13

标签: sql sql-server tsql select

我有下表:

CREATE TABLE studenttest 
(
    YEAR INT,
    DEPT VARCHAR(5),
    SEM INT,
    REGNO INT,
    NAME VARCHAR(20),
    ENGLISH VARCHAR(2),
    MATHS VARCHAR(2),
    PHYSICS VARCHAR(2),
    CHEMISTRY VARCHAR(2),
    EG VARCHAR(2),
    FOC VARCHAR(2),
    LAB1 VARCHAR(2),
    LAB2 VARCHAR(2),
    LAB3 VARCHAR(2)
)

使用以下数据:

INSERT INTO studenttest 
 values
 (2010,'cse',3,1,'saravaanan','a','b','c','d','ra','f','g','h','i'),   
 (2010,'cse',3,2,'raja','ra','b','c','d','e','f','g','h','i'),
 (2010,'cse',3,3,'selvam','a','b','c','d','e','f','g','h','i')

我想查询这些数据,以获得所有没有等级" ra"在任何的主题中。

6 个答案:

答案 0 :(得分:5)

SELECT  a.*
FROM    studenttest a
        LEFT JOIN
        (
          SELECT regno, name
          FROM studenttest
          WHERE ENGLISH = 'ra' OR
                MATHS = 'ra' OR
                PHYSICS = 'ra' OR
                CHEMISTRY    = 'ra' OR
                EG  = 'ra' OR   
                FOC = 'ra' OR   
                LAB1 = 'ra' OR  
                LAB2 = 'ra' OR  
                LAB3 = 'ra'
        ) b ON a.regno = b.regno 
               AND a.Name = b.Name   -- this line is OPTIONAL
WHERE   b.regno IS NULL

答案 1 :(得分:4)

只需使用UNPIVOTCTE作为@Mahmoud's version的替代方法,也可以使用UNION ALL完成此操作:

SELECT Name
FROM
(
  SELECT Name, ENGLISH grade, 'English' subject
  from studenttest
  union all
  SELECT Name, MATHS grade, 'MATHS' subject
  from studenttest
  union all
  SELECT Name, PHYSICS grade, 'PHYSICS' subject
  from studenttest
  union all
  SELECT Name, CHEMISTRY grade, 'CHEMISTRY' subject
  from studenttest
  union all
  SELECT Name, EG grade, 'EG' subject
  from studenttest
  union all
  SELECT Name, FOC grade, 'FOC' subject
  from studenttest
  union all
  SELECT Name, LAB1 grade, 'LAB1' subject
  from studenttest
  union all
  SELECT Name, LAB2 grade, 'LAB2' subject
  from studenttest
  union all
  SELECT Name, LAB3 grade, 'LAB3' subject
  from studenttest
) un
where grade <> 'ra'
group by name
having count(grade) = 9

UNPIVOT执行与UNION ALL执行的代码相同的步骤。当您的数据未像表那样进行规范化时,这些查询会很有效。

如果您想要所有数据(不仅仅是名称),您可以再次将上述查询重新加入到表中:

select *
from studenttest t
inner join
(
  SELECT Name
  FROM
  (
    SELECT Name, ENGLISH grade, 'English' subject
    from studenttest
    union all
    SELECT Name, MATHS grade, 'MATHS' subject
    from studenttest
    union all
    SELECT Name, PHYSICS grade, 'PHYSICS' subject
    from studenttest
    union all
    SELECT Name, CHEMISTRY grade, 'CHEMISTRY' subject
    from studenttest
    union all
    SELECT Name, EG grade, 'EG' subject
    from studenttest
    union all
    SELECT Name, FOC grade, 'FOC' subject
    from studenttest
    union all
    SELECT Name, LAB1 grade, 'LAB1' subject
    from studenttest
    union all
    SELECT Name, LAB2 grade, 'LAB2' subject
    from studenttest
    union all
    SELECT Name, LAB3 grade, 'LAB3' subject
    from studenttest
  ) un
  where grade <> 'ra'
  group by name
  having count(grade) = 9
) t2
  on t.name = t2.name

请参阅SQL Fiddle with Demo

答案 2 :(得分:3)

你可以这样做:

WITH AllSubjectsGrades
AS
(
  SELECT Name, Grade
  FROM
  (
    SELECT 
      Name, 
      ENGLISH, MATHS, PHYSICS,  CHEMISTRY,
      EG,  FOC, LAB1, LAB2, LAB3
    FROM studenttest
  ) t
  UNPIVOT
  (
    grade
    FOR Subject IN(ENGLISH, MATHS, PHYSICS, CHEMISTRY,
                   EG,  FOC, LAB1, LAB2, LAB3)
  ) u
)
SELECT Name
FROM AllSubjectsGrades
WHERE GRADE <> 'ra'
GROUP BY Name
HAVING COUNT(GRADE) = 9;

SQL Fiddle Demo

这将只向您提供selvam学生,因为saravaanan的{​​{1}}中的'ra'和英语中的EG有'ra'。但raja从未在任何主题中获得selvam。那是什么

ra

执行。

答案 3 :(得分:2)

你是说这个吗?

SELECT * 
FROM studenttest
WHERE 
    ENGLISH <> 'ra' AND
    MATHS <> 'ra' AND
    PHYSICS <> 'ra' AND
    CHEMISTRY    <> 'ra' AND
    EG  <> 'ra' AND   
    FOC <> 'ra' AND   
    LAB1 <> 'ra' AND  
    LAB2 <> 'ra' AND
    LAB3 <> 'ra'

答案 4 :(得分:2)

还有一种方法:

SELECT * 
FROM studenttest
WHERE 'ra' NOT IN 
      (ENGLISH, MATHS, PHYSICS, CHEMISTRY, EG, FOC, LAB1, LAB2, LAB3) ;

答案 5 :(得分:1)

SELECT *
FROM studenttest
WHERE ENGLISH + ',' + MATHS + ',' + PHYSICS + ',' + CHEMISTRY + ',' + EG + ',' + FOC + ',' + LAB1 + ',' + LAB2 + ',' + LAB3 NOT LIKE '%ra%'

SQLFiddle上的演示