7

次の表があります。

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」でないすべての学生の結果セットを取得したいと考えています。

4

6 に答える 6

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
于 2012-12-29T06:51:13.807 に答える
4

と を使用した@Mahmoud のバージョンの代わりにUNPIVOTCTEこれは を使用して行うこともできます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 を参照してください

于 2012-12-29T13:44:31.990 に答える
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 フィドルのデモ

には「ra」が含まれ、英語には「ra」が含まれているselvamため、これにより Student のみが表示されます。しかし、どの科目にも入ったことはありません。そして、それは何ですかsaravaananEGrajaselvamra

GROUP BY Name
HAVING COUNT(GRADE) = 9;

行う。

于 2012-12-29T06:52:45.743 に答える
2

もう1つの方法:

SELECT * 
FROM studenttest
WHERE 'ra' NOT IN 
      (ENGLISH, MATHS, PHYSICS, CHEMISTRY, EG, FOC, LAB1, LAB2, LAB3) ;
于 2012-12-29T13:50:44.187 に答える
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'
于 2012-12-29T06:58:37.650 に答える
1
SELECT *
FROM studenttest
WHERE ENGLISH + ',' + MATHS + ',' + PHYSICS + ',' + CHEMISTRY + ',' + EG + ',' + FOC + ',' + LAB1 + ',' + LAB2 + ',' + LAB3 NOT LIKE '%ra%'

SQLFiddle のデモ

于 2012-12-29T09:57:41.857 に答える