【问题标题】:Oracle SQL to convert related rows to a single row [duplicate]Oracle SQL将相关行转换为单行[重复]
【发布时间】:2014-12-19 19:46:12
【问题描述】:

我有一个如下所示的表格:

RollNo Name  Subject Score

021    Gokul English 65
021    Gokul French  75
021    Gokul Germany 85
021    Gokul Spanish 95
031    Karth English 51
031    Karth French  61
031    Karth Germany 71

我想查询此表,以便将相关行转换为单行,如下所示:

RollNo Name    English_score   French_score   Germany_score   Spanish_score

021    Gokul        65               75             85              95
031    Karth        51               61             71

我怎样才能做到这一点?

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    使用PIVOT

    SELECT * FROM
    (SELECT "RollNo", "Name", 
    "Subject", "score" FROM Table1)  T
    PIVOT
    ( max("score") for "Subject" in
       ('English' as English_score,
        'French'  as French_score,
        'Germany' as Germany_score,
        'Spanish' as Spanishh_score
        )
    )
    

    【讨论】:

      【解决方案2】:

      这是一种使用联接而不是枢轴的方法:

      SELECT t.RollNo, t.Name,
             t2.Score AS English_score,
             t3.Score AS French_score,
             t4.Score AS Germany_score,
             t5.Score AS Spanish_score
      FROM 
      (
        SELECT DISTINCT RollNo, Name
        FROM table
      ) AS t
      LEFT JOIN table t2 ON t.RollNo = t2.RollNo AND t2.Subject = 'English'
      LEFT JOIN table t3 ON t.RollNo = t3.RollNo AND t3.Subject = 'French'
      LEFT JOIN table t4 ON t.RollNo = t4.RollNo AND t4.Subject = 'Germany'
      LEFT JOIN table t5 ON t.RollNo = t5.RollNo AND t5.Subject = 'Spanish'
      

      【讨论】:

        【解决方案3】:

        如果您使用的是 Oracle 数据库 11g 或更高版本,则可以使用pivot。试试这个:

        SELECT * FROM (
          SELECT RollNo, Name, Subject, Score
          FROM table1
        )
        PIVOT (MAX(Score) AS Score FOR (Subject) 
         IN (
           'English' AS English, 
           'French' AS French, 
           'Germany' AS Germany, 
           'Spanish' AS Spanish
           )
         );
        

        Sample SQL Fiddle

        【讨论】:

          【解决方案4】:

          试试这个

          select 
              rollno, name, subject, 
              english_score, french_score, german_source, spanish_score
          from
              (select 
                   RollNo, name, subject,
                   decode(subject, "English", score, "") English_score,
                   decode(subject, "French", score, "") French_score,
                   decode(subject, "Germany", score, "") German_score,
                   decode(subject, "Spanish", score, "") Spanish_score,
                   count(*)
               from 
                   tbl
               group by 
                   RollNo, name, subject,
                   decode(subject, "English", score, "") English_score,
                   decode(subject, "French", score, "") French_score,
                   decode(subject, "Germany", score, "") German_score,
                   decode(subject, "Spanish", score, "") Spanish_score)) tbl2
          

          Oracle 中有一个选项可以不显示列。我认为这是“noprint”,我只是不记得语法。你可以把它放在count(*)旁边:

          Select 
              RollNo, name, subject,
              decode(subject, "English", score, "") English_score,
              decode(subject, "French", score, "") French_score,
              decode(subject, "Germany", score, "") German_score,
              decode(subject, "Spanish", score, "") Spanish_score,
              count(*) noprint
          from 
              tbl
          group by 
              RollNo, name, subject,
              decode(subject, "English", score, "") English_score,
              decode(subject, "French", score, "") French_score,
              decode(subject, "Germany", score, "") German_score,
              decode(subject, "Spanish", score, "") Spanish_score
          

          【讨论】:

          • 感谢朋友们。我们可以使用 XMLAGG(xmlelement(E , score || ' ')) 后跟 Group by 命令来达到预期的效果。
          猜你喜欢
          • 2020-02-23
          • 2021-08-07
          • 2019-10-14
          • 2020-07-01
          • 2017-10-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多