MSSQL縱列轉橫列

2022-01-13 15:41:17 字數 4521 閱讀 4151

在工作中我們一般會遇到將縱列轉橫列的需求,具體**:

1.建表

create

table

[dbo

].[acrosschangeendlong](

[id][

int]

identity(1,1) not

null

,

[name][

nvarchar

](50) not

null

,

[subject][

nvarchar

](50) not

null

,

[score][

int]

notnull,

constraint

[pk_acrosschangeendlong

]primary

keyclustered

( [id

]asc

)with (pad_index =

off, statistics_norecompute =

off, ignore_dup_key =

off, allow_row_locks =

on, allow_page_locks =

on) on

[primary])

on[primary]go

setidentity_insert

[dbo

].[acrosschangeendlong]on

goinsert

[dbo

].[acrosschangeendlong

] ([

id], [

name

], [

subject

], [

score

]) values (1, n'

張三', n'

語文', 100)go

insert

[dbo

].[acrosschangeendlong

] ([

id], [

name

], [

subject

], [

score

]) values (3, n'

李四', n'

語文', 80)go

insert

[dbo

].[acrosschangeendlong

] ([

id], [

name

], [

subject

], [

score

]) values (4, n'

張三', n'

英語', 80)go

insert

[dbo

].[acrosschangeendlong

] ([

id], [

name

], [

subject

], [

score

]) values (5, n'

李四', n'

英語', 50)go

insert

[dbo

].[acrosschangeendlong

] ([

id], [

name

], [

subject

], [

score

]) values (6, n'

張三', n'

數學', 55)go

insert

[dbo

].[acrosschangeendlong

] ([

id], [

name

], [

subject

], [

score

]) values (7, n'

李四', n'

數學', 34)go

setidentity_insert

[dbo

].[acrosschangeendlong

]off

goalter

table

[dbo

].[acrosschangeendlong

]add

constraint

[df_acrosschangeendlong_score

]default ((0)) for

[score

]go

2.具體sql

select  name as'姓名

',max(case

[subject

]when'語文

'then

score

else

0end) as'語文

',max(case

[subject

]when'英語

'then

score

else

0end) as'英語

',max(case

[subject

]when'數學

'then

score

else

0end) as'數學

'from

dbo.acrosschangeendlong

group

by name order by name

如果到時候增加了科目,比如增加了化學,這時候為了再次修改,我們可以弄成動態的(根據列自動增加),這裡使用的是動態拼接sql,會根據科目的增加而增加列,具體sql如下

declare

@sql

varchar(8000

)set

@sql='

select [name],

'select

@sql

=@sql+'

sum(case [subject] when

'''+

[subject]+

'''then [score] else 0 end) as

'''+

[subject]+

''',

'from ( select

distinct

[subject

]from

dbo.acrosschangeendlong

) asa

select

@sql

=left(@sql, len(@sql) -1)

+'from [acrosschangeendlong] group by [name]

'print ( @sql

)exec(@sql)

另外在sql server 2005之後有了乙個專門的pivot 和 unpivot 關係運算子做行列之間的轉換

注意:這種不能用於 eg: case [subject] when '數學' then score else 0 end 如果then 後面是1(統計的時候會用到)而不是具體欄位的時候,使用會報錯,具體sql如下

select

*from ( select name as'姓名

',subject ,

score

from

dbo.acrosschangeendlong

) p pivot ( max

(score) for subject in ( [

數學], [

英語], [

語文] ) ) as

pvtorder

by pvt.姓名

使用pivot動態拼接sql:

declare

@sql_str

varchar(max

)declare

@sql_col

varchar(max

)select

@sql_col

=isnull(@sql_col+'

,', '') +

quotename([

subject])

from

dbo.acrosschangeendlong

groupby[

subject

]set

@sql_str='

select * from (

select [name],[subject],[score] from [acrosschangeendlong]) p pivot

(sum([score]) for [subject] in ( '+

@sql_col+'

) ) as pvt

order by pvt.[name]

'print ( @sql_str

)exec (@sql_str)

具體效果:  

操作Excel縱列 數值轉字母

public static string getexcelcolumnlabel int index while index 0 return rs 測試了一下 system.out.println getexcelcolumnlabel 0 輸出 system.out.println getexc...

MSSQL 本週 本月 本年 轉 修)

本週 起 dateadd wk,datediff wk,0,getdate 1 止 dateadd wk,datediff wk,0,getdate 6 本月 起 dateadd mm,datediff mm,0,getdate 0 止 dateadd ms,3,dateadd mm,datedif...

MS SQL 日期轉字元全格式

select convert varchar 100 getdate 0 07 16 2012 10 57am select convert varchar 100 getdate 1 07 16 12 select convert varchar 100 getdate 2 12.07.16 se...