×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

If you want do it in One SQL, then there is some limitation. If you know all the colume values, then you can do it. Otherwise you can not. Please check inside.

本文发表在 rolia.net 枫下论坛I do not know if I understand your question clearly, but I can give you one example how to deal with this kind of problem.

Table A,
Student No, Course No, Score
1111, A11 80
2222, A11 85
3333, A11 70
1111, A12 50
2222 A12 60
3333 A12 40
2222 A13 50
3333 A13 90

You want to turn into the format like this
Student NO, Score of CourseA1, Score of CourseA2, Score of CourseA3
1111 80 50
2222 85 60 50
3333 70 40 90

Here you know the course will only be A1,A2,A3, this is the key point here.
select aliasppp.studentNO,
( select aliasp.score
from A aliasp
where aliasp.studentNO = aliasppp.studentNO
and aliasp.course = 'A1) ,
( select aliasp.score
from A aliasp
where aliasp.studentNO = aliasppp.studentNO
and aliasp.course = 'A2) ,
( select aliasp.score
from A aliasp
where aliasp.studentNO = aliasppp.studentNO
and aliasp.course = 'A3)
FROM A aliasppp更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / small question: how to turn column into row? I use Oracle Thanks
    I had that script, but just can't find it now.
    • What you mean here? turn column names into rows or turn one row data into rows, each row contains a column data?
      • Hehe
        A column X has value a1,a2,b1,B2 .... etc.
        select X from table, the result will display like
        X
        ---
        a1
        a2
        b1
        B2 ....
        and What I want is to display like this
        a1 a2 b1 b2

        Thanks,
        • Open the table multi-times
        • If you want do it in One SQL, then there is some limitation. If you know all the colume values, then you can do it. Otherwise you can not. Please check inside.
          本文发表在 rolia.net 枫下论坛I do not know if I understand your question clearly, but I can give you one example how to deal with this kind of problem.

          Table A,
          Student No, Course No, Score
          1111, A11 80
          2222, A11 85
          3333, A11 70
          1111, A12 50
          2222 A12 60
          3333 A12 40
          2222 A13 50
          3333 A13 90

          You want to turn into the format like this
          Student NO, Score of CourseA1, Score of CourseA2, Score of CourseA3
          1111 80 50
          2222 85 60 50
          3333 70 40 90

          Here you know the course will only be A1,A2,A3, this is the key point here.
          select aliasppp.studentNO,
          ( select aliasp.score
          from A aliasp
          where aliasp.studentNO = aliasppp.studentNO
          and aliasp.course = 'A1) ,
          ( select aliasp.score
          from A aliasp
          where aliasp.studentNO = aliasppp.studentNO
          and aliasp.course = 'A2) ,
          ( select aliasp.score
          from A aliasp
          where aliasp.studentNO = aliasppp.studentNO
          and aliasp.course = 'A3)
          FROM A aliasppp更多精彩文章及讨论,请光临枫下论坛 rolia.net
          • if it is a single column table, what can you do?...
            • Then it does not make sense to turn it into one row. I do not think you have any real examples having this kind of requirement. Anyway, you still can do it through multi-SQL .
              • Can you solve my 'silly' problem...(This would be a interesting script)
                Here is my table in Oracle DB:
                Col
                -----
                1
                2
                3
                4
                5
                6
                7
                I am expecting the output as
                My_Output
                --------------
                1234567
                • If you think it is a stupid question, then why I should bother to fix it. I do not want to be looked as a geek. Do something useful please.