sql server中的 行轉列

來源:互聯網
上載者:User

標籤:style   blog   color   io   ar   strong   for   sp   div   

PIVOT syntax:

       SELECT ..... FROM  pivoted_table        pivoted_table ::=  table_source PIVOT < pivot_clausetable_alias         table_source ::= table,view, sub-query,XML......, 即SQL語句From關鍵字後可以跟的所有東西,定義太複雜,這裡就不寫了         pivot_clause ::=(   aggregate_function  (  value_column  ) 
                                    FOR  pivot_column 
                                    IN  ( <column_list>  ) 
                                 ) 
      UNPIVOT syntax:       SELECT ..... FROM  unpivoted_table        unpivoted_table ::=  table_source UNPIVOT < unpivot_clausetable_alias         table_source ::= Table, view, sub-query, XML..., 即SQL語句From關鍵字後可以跟的所有東西,定義太複雜,這裡就不寫了        unpivot _clause ::= value_column FOR  pivot_column IN ( <column_list> ) )      PIVOT example:
    
CREATE TABLE Score ( StuNo varchar(10), StuName varchar(50), CourseName varchar(50), Score int);    GO    INSERT INTO Score (StuNo, StuName, CourseName, Score) VALUES (‘1‘, ‘Tom‘, ‘Math‘, 80);    INSERT INTO Score (StuNo, StuName, CourseName, Score) VALUES (‘1‘, ‘Tom‘, ‘English‘, 82);    INSERT INTO Score (StuNo, StuName, CourseName, Score) VALUES (‘1‘, ‘Tom‘, ‘Geography‘, 84);    INSERT INTO Score (StuNo, StuName, CourseName, Score) VALUES (‘2‘, ‘Jone‘, ‘Math‘, 79);    INSERT INTO Score (StuNo, StuName, CourseName, Score) VALUES (‘2‘, ‘Jone‘, ‘English‘, 88);    INSERT INTO Score (StuNo, StuName, CourseName, Score) VALUES (‘2‘, ‘Jone‘, ‘Geography‘, 86);    GO     SELECT * FROM Score;

 

      StuNo StuName CourseName Score
      1       Tom     Math       80
      1       Tom     English    82
      1       Tom     Geography  84
      2       Jone    Math       79
      2       Jone    English    88
      2       Jone    Geography  86     
--Change row to column:
SELECT StuNo, StuName, Math, English, Geography FROM Score PIVOT ( MAX(Score) FOR CourseName in (Math, English, Geography) ) AS ScoreList ORDER BY StuNo;

 

     StuNo  StuName  Math English  Geography
      1       Tom      80    82       84
      2       Jone     79    88       86    
 DROP TABLE Score;    GO

 

    UNPIVOT example:
    
CREATE TABLE ScoreList ( StuNo varchar(10), StuName varchar(50), Math int, English int, Geography int);    GO    INSERT INTO ScoreList (StuNo, StuName, Math, English, Geography) VALUES (‘1‘, ‘Tom‘, 80, 82, 84);    INSERT INTO ScoreList (StuNo, StuName, Math, English, Geography) VALUES (‘2‘, ‘Jone‘, 79, 88, 86);    GO     SELECT * FROM ScoreList;

 

     StuNo  StuName  Math English  Geography
      1       Tom      80    82       84
      2       Jone     79    88       86      
--Change column values to row:    SELECT StuNo, StuName, CourseName, Score    FROM ScoreList    UNPIVOT ( Score FOR CourseName in (Math, English, Geography) ) AS ScorePvtTable    ORDER BY StuNo; 

 

    StuNo StuName CourseName Score
      1     Tom     Math       80
      1     Tom     English    82
      1     Tom     Geography  84
      2     Jone    Math       79
      2     Jone    English    88
      2     Jone    Geography  86  
   DROP TABLE ScoreList;    GO

 

sql server中的 行轉列

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.