Mysql-如何根据quarter列将行更改为列

时间:2013-07-25 06:18:32

标签: mysql

我想根据季度将行更改为cloms。

我的示例表如下

Student name    subject standard    marks   quarter
Harry   Maths   class1  19  q1
Harry   Maths   class1  19  q2
Harry   Maths   class1  19  q3
Harry   Maths   class1  19  q4
Harry   science class1  18  q1
Harry   science class1  18  q2
Harry   science class1  18  q3
Harry   science class1  18  q4
Harry   social  class1  19  q1
Harry   social  class1  19  q2
Harry   social  class1  19  q3
Harry   social  class1  19  q4
Raj     Maths   class1  19  q1
Raj    Maths    class1  19  q2
Raj    Maths    class1  19  q3
Raj    Maths    class1  19  q4
Raj    science  class1  18  q1
Raj    science  class1  18  q2
Raj    science  class1  18  q3
Raj    science  class1  18  q4
Raj    social   class1  19  q1
Raj    social   class1  19  q2
Raj    social   class1  19  q3
Raj    social   class1  19  q4

输出应为

Student name    subject standard    marks   quarter subject marks1  quarter1        subject marks1  quarter2    subject marks1  quarter3
Harry   Maths   class1  19  q1  Maths   19  q2  Maths   19  q3      Maths   19  q4
Harry   science class1  18  q1  science 18  q2  science 18  q3  science 18  q4
Harry   social  class1  19  q1  social  19  q2  social  19  q3      social  19  q4
Raj Maths   class1  19  q1  Maths   19  q2  Maths   19  q3  Maths   19  q4
Raj science class1  18  q1  science 18  q2  science 18  q3  science 18  q4
Raj social  class1  19  q1  social  19  q2  social  19  q3  social  19  q4

我尝试了很多方法,例如使用where =' q1'组合两个select语句。但是,它给出了多行。 请帮帮我

请不要介意我的表格格式希望您理解

2 个答案:

答案 0 :(得分:0)

根据以下各行的不同方面的名称,主题,标准加入表格: -

SELECT *
  FROM tbl t1
  JOIN tbl t2
    ON t1.name = t2.name and t1.subject = t2.subject and t1.standard = t1.standard
  JOIN tbl t3
    ON t2.name = t3.name and t2.subject = t3.subject and t2.standard = t2.standard
 WHERE t1.quarter = 'q1' and t2.quarter = 'q2' and t3.quarter = 'q3'

答案 1 :(得分:0)

如果输出正确,请尝试

SELECT student_name,
       standard,
         'q1' quarter1,
         MIN(CASE WHEN quarter = 'q1' THEN subject END) subject1,
         MIN(CASE WHEN quarter = 'q1' THEN marks   END) marks1,
         'q2' quarter2,
         MIN(CASE WHEN quarter = 'q2' THEN subject END) subject2,
         MIN(CASE WHEN quarter = 'q2' THEN marks   END) marks2,
         'q3' quarter3,
         MIN(CASE WHEN quarter = 'q3' THEN subject END) subject3,
         MIN(CASE WHEN quarter = 'q3' THEN marks   END) marks3,
         'q4' quarter4,
         MIN(CASE WHEN quarter = 'q4' THEN subject END) subject4,
         MIN(CASE WHEN quarter = 'q4' THEN marks   END) marks4
  FROM Table1
 GROUP BY student_name, standard, subject

示例输出:

| STUDENT_NAME | STANDARD | QUARTER1 | SUBJECT1 | MARKS1 | QUARTER2 | SUBJECT2 | MARKS2 | QUARTER3 | SUBJECT3 | MARKS3 | QUARTER4 | SUBJECT4 | MARKS4 |
-------------------------------------------------------------------------------------------------------------------------------------------------------
|        Harry |   class1 |       q1 |    Maths |     19 |       q2 |    Maths |     19 |       q3 |    Maths |     19 |       q4 |    Maths |     19 |
|        Harry |   class1 |       q1 |  science |     18 |       q2 |  science |     18 |       q3 |  science |     18 |       q4 |  science |     18 |
|        Harry |   class1 |       q1 |   social |     19 |       q2 |   social |     19 |       q3 |   social |     19 |       q4 |   social |     19 |
|          Raj |   class1 |       q1 |    Maths |     19 |       q2 |    Maths |     19 |       q3 |    Maths |     19 |       q4 |    Maths |     19 |
|          Raj |   class1 |       q1 |  science |     18 |       q2 |  science |     18 |       q3 |  science |     18 |       q4 |  science |     18 |
|          Raj |   class1 |       q1 |   social |     19 |       q2 |   social |     19 |       q3 |   social |     19 |       q4 |   social |     19 |

这是 SQLFiddle 演示