SQL Server行轉列的方法解析

2022-09-21 09:57:09 字數 799 閱讀 3625

話不多說,請看**:

select 非透視的列》,

[第乙個透視的列] as 列名稱》,

[第二個透視的列] as 列名稱》,

...[最後乙個透視的列] as 列名稱》,

from

(生成資料的 select )

as 源查詢的別名》

pivot

( 聚合函式》(要)

for[包含要成為列標題的值的列》]

in ( [第乙個透視的列], [第二個透視的列],

... [最後乙個透視的列])

) as 透視表的別名》

可選的 order by ;

*/declare @tb table

(cid int

,cname varchar(10)

)insert into @tb

select 1,'aa'

union all

select 2,'bb'

select * from @tb

declare @ifvpgubxgidaa int

,@idbb int

select aa,bb

from(

select cid,cname from @tb

)as tt

pivot(

max(cid) for cname in([aa],bb)

)piv

本文標題: sql server行轉列的方法解析

本文位址: /shujuku/mssql/182778.html

SqlServer經典行轉列 討論

use cubedemo goset nocount on 銷售模組 if object id n businesscommon salemodule n u isnot null drop table businesscommon salemodule gocreate table busines...

SQL server 行轉列 列轉行

1.簡單案例 create table student sid int primary key identity 1,1 主鍵自增 sname varchar 20 學生姓名 select from student create table class cid int primary key ide...

SQL SERVER 行轉列 列轉行

第一步 建立臨時表結構 create table student 建立臨時表 stuname nvarchar 20 學生名稱 stusubject nvarchar 20 考試科目 stuscore int 考試成績 drop table student 刪除臨時表 select from stu...