www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當前位置:首頁 > 芯聞號 > 充電吧
[導讀]/*標題:普通行列轉換(version 2.0)作者:愛新覺羅.毓華 時間:2008-03-09地點:廣東深圳說明:普通行列轉換(version 1.0)僅針對sql server 2000提供靜態(tài)和

/*
標題:普通行列轉換(version 2.0)
作者:愛新覺羅.毓華
時間:2008-03-09
地點:廣東深圳
說明:普通行列轉換(version 1.0)僅針對sql server 2000提供靜態(tài)和動態(tài)寫法,version 2.0增加sql server 2005的有關寫法。

問題:假設有張學生成績表(tb)如下:
姓名 課程 分數(shù)
張三 語文 74
張三 數(shù)學 83
張三 物理 93
李四 語文 74
李四 數(shù)學 84
李四 物理 94
想變成(得到如下結果):
姓名 語文 數(shù)學 物理
---- ---- ---- ----
李四 74?? 84?? 94
張三 74?? 83?? 93
-------------------
*/

create table tb(姓名 varchar(10) , 課程 varchar(10) , 分數(shù) int)
insert into tb values('張三' , '語文' , 74)
insert into tb values('張三' , '數(shù)學' , 83)
insert into tb values('張三' , '物理' , 93)
insert into tb values('李四' , '語文' , 74)
insert into tb values('李四' , '數(shù)學' , 84)
insert into tb values('李四' , '物理' , 94)
go

--SQL SERVER 2000 靜態(tài)SQL,指課程只有語文、數(shù)學、物理這三門課程。(以下同)
select 姓名 as 姓名 ,
? max(case 課程 when '語文' then 分數(shù) else 0 end) 語文,
? max(case 課程 when '數(shù)學' then 分數(shù) else 0 end) 數(shù)學,
? max(case 課程 when '物理' then 分數(shù) else 0 end) 物理
from tb
group by 姓名

--SQL SERVER 2000 動態(tài)SQL,指課程不止語文、數(shù)學、物理這三門課程。(以下同)
declare @sql varchar(8000)
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 課程 when ''' + 課程 + ''' then 分數(shù) else 0 end) [' + 課程 + ']'
from (select distinct 課程 from tb) as a
set @sql = @sql + ' from tb group by 姓名'
exec(@sql)

--SQL SERVER 2005 靜態(tài)SQL。
select * from (select * from tb) a pivot (max(分數(shù)) for 課程 in (語文,數(shù)學,物理)) b

--SQL SERVER 2005 動態(tài)SQL。
declare @sql varchar(8000)
select @sql = isnull(@sql + '],[' , '') + 課程 from tb group by 課程
set @sql = '[' + @sql + ']'
exec ('select * from (select * from tb) a pivot (max(分數(shù)) for 課程 in (' + @sql + ')) b')

---------------------------------

/*
問題:在上述結果的基礎上加平均分,總分,得到如下結果:
姓名 語文 數(shù)學 物理 平均分 總分
---- ---- ---- ---- ------ ----
李四 74?? 84?? 94?? 84.00? 252
張三 74?? 83?? 93?? 83.33? 250
*/

--SQL SERVER 2000 靜態(tài)SQL。
select 姓名 姓名,
? max(case 課程 when '語文' then 分數(shù) else 0 end) 語文,
? max(case 課程 when '數(shù)學' then 分數(shù) else 0 end) 數(shù)學,
? max(case 課程 when '物理' then 分數(shù) else 0 end) 物理,
? cast(avg(分數(shù)*1.0) as decimal(18,2)) 平均分,
? sum(分數(shù)) 總分
from tb
group by 姓名

--SQL SERVER 2000 動態(tài)SQL。
declare @sql varchar(8000)
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 課程 when ''' + 課程 + ''' then 分數(shù) else 0 end) [' + 課程 + ']'
from (select distinct 課程 from tb) as a
set @sql = @sql + ' , cast(avg(分數(shù)*1.0) as decimal(18,2)) 平均分 , sum(分數(shù)) 總分 from tb group by 姓名'
exec(@sql)

--SQL SERVER 2005 靜態(tài)SQL。
select m.* , n.平均分 , n.總分 from
(select * from (select * from tb) a pivot (max(分數(shù)) for 課程 in (語文,數(shù)學,物理)) b) m,
(select 姓名 , cast(avg(分數(shù)*1.0) as decimal(18,2)) 平均分 , sum(分數(shù)) 總分 from tb group by 姓名) n
where m.姓名 = n.姓名

--SQL SERVER 2005 動態(tài)SQL。
declare @sql varchar(8000)
select @sql = isnull(@sql + ',' , '') + 課程 from tb group by 課程
exec ('select m.* , n.平均分 , n.總分 from
(select * from (select * from tb) a pivot (max(分數(shù)) for 課程 in (' + @sql + ')) b) m ,
(select 姓名 , cast(avg(分數(shù)*1.0) as decimal(18,2)) 平均分 , sum(分數(shù)) 總分 from tb group by 姓名) n
where m.姓名 = n.姓名')

drop table tb???

------------------
------------------

/*
問題:如果上述兩表互相換一下:即表結構和數(shù)據(jù)為:
姓名 語文 數(shù)學 物理
張三 74  83  93
李四 74  84  94
想變成(得到如下結果):
姓名 課程 分數(shù)
---- ---- ----
李四 語文 74
李四 數(shù)學 84
李四 物理 94
張三 語文 74
張三 數(shù)學 83
張三 物理 93
--------------
*/

create table tb(姓名 varchar(10) , 語文 int , 數(shù)學 int , 物理 int)
insert into tb values('張三',74,83,93)
insert into tb values('李四',74,84,94)
go

--SQL SERVER 2000 靜態(tài)SQL。
select * from
(
select 姓名 , 課程 = '語文' , 分數(shù) = 語文 from tb
union all
select 姓名 , 課程 = '數(shù)學' , 分數(shù) = 數(shù)學 from tb
union all
select 姓名 , 課程 = '物理' , 分數(shù) = 物理 from tb
) t
order by 姓名 , case 課程 when '語文' then 1 when '數(shù)學' then 2 when '物理' then 3 end

--SQL SERVER 2000 動態(tài)SQL。
--調用系統(tǒng)表動態(tài)生態(tài)。
declare @sql varchar(8000)
select @sql = isnull(@sql + ' union all ' , '' ) + ' select 姓名 , [課程] = ' + quotename(Name , '''') + ' , [分數(shù)] = ' + quotename(Name) + ' from tb'
from syscolumns
where name! = N'姓名' and ID = object_id('tb') --表名tb,不包含列名為姓名的其它列
order by colid asc
exec(@sql + ' order by 姓名 ')

--SQL SERVER 2005 動態(tài)SQL。
select 姓名 , 課程 , 分數(shù) from tb unpivot (分數(shù) for 課程 in([語文] , [數(shù)學] , [物理])) t

--SQL SERVER 2005 動態(tài)SQL,同SQL SERVER 2000 動態(tài)SQL。

--------------------
/*
問題:在上述的結果上加個平均分,總分,得到如下結果:
姓名 課程?? 分數(shù)
---- ------ ------
李四 語文?? 74.00
李四 數(shù)學?? 84.00
李四 物理?? 94.00
李四 平均分 84.00
李四 總分?? 252.00
張三 語文?? 74.00
張三 數(shù)學?? 83.00
張三 物理?? 93.00
張三 平均分 83.33
張三 總分?? 250.00
------------------
*/

select * from
(
select 姓名 as 姓名 , 課程 = '語文' , 分數(shù) = 語文 from tb
union all
select 姓名 as 姓名 , 課程 = '數(shù)學' , 分數(shù) = 數(shù)學 from tb
union all
select 姓名 as 姓名 , 課程 = '物理' , 分數(shù) = 物理 from tb
union all
select 姓名 as 姓名 , 課程 = '平均分' , 分數(shù) = cast((語文 + 數(shù)學 + 物理)*1.0/3 as decimal(18,2)) from tb
union all
select 姓名 as 姓名 , 課程 = '總分' , 分數(shù) = 語文 + 數(shù)學 + 物理 from tb
) t
order by 姓名 , case 課程 when '語文' then 1 when '數(shù)學' then 2 when '物理' then 3 when '平均分' then 4 when '總分' then 5 end

drop table tb

================================================================

?

create table A(id char(3), num1 int, num2 int, num3 int, num4 int)
insert A select '001', 80, 90, 50, 60
insert A select '002', 84, 70, 60, 82
go
--SQL2005實現(xiàn)方法:
select * from A
unpivot
(num for col in([num1],[num2],[num3],[num4]))T2)tmp

--SQL2000實現(xiàn):
---調用系統(tǒng)表動態(tài)生態(tài)
declare @s nvarchar(4000)
select @s=isnull(@s+' union all ','')+' select ID,[num]='+quotename(Name,'''')+',Qty='+quotename(Name)+' from A'
from syscolumns
where Name!=N'ID' and ID=object_id('A')--表名A,不包含列名為ID的其它列
order by colid asc
exec(@s+' order by ID asc,[num] asc')

--生成的靜態(tài)語句
select ID,[num]='num1',Qty=[num1] from A union all
select ID,[num]='num2',Qty=[num2] from A union all
select ID,[num]='num3',Qty=[num3] from A union all
select ID,[num]='num4',Qty=[num4] from A
order by ID asc,[num] asc

/*

ID num Qty
---- ---- -----------
001 num1 80
001 num2 90
001 num3 50
001 num4 60
002 num1 84
002 num2 70
002 num3 60
002 num4 82
------------------------------
*/


--動態(tài)方法:
declare @s nvarchar(4000)
select @s=isnull(@s+' union all ','')+' select ID,[num]='+quotename(Name)+' from A'
from syscolumns
where Name!=N'ID' and ID=object_id('A')
order by colid asc
exec(@s+' order by ID asc')

--生成的語句如下:

select ID,[num]=[num1] from A union all
select ID,[num]=[num2] from A union all
select ID,[num]=[num3] from A union all
select ID,[num]=[num4] from A
order by ID asc,[num] asc



/*
ID num
---- -----------
001 80
001 90
001 50
001 60
002 82
002 60
002 70
002 84
*/


---drop table A

====================================================================

?

/*
將表數(shù)據(jù)旋轉90度(2007-11-19于海南三亞)

將下表數(shù)據(jù):
A??????????????????? b?????????? c?????????? d?????????? e??????????
-------------------- ----------- ----------- ----------- -----------
x??????????????????? 1?????????? 2?????????? 3?????????? 4
y??????????????????? 5?????????? 6?????????? 7?????????? 8
z??????????????????? 9?????????? 10????????? 11????????? 12

轉化成如下結果:
a??????????????????? x????????? y????????? z?????????
-------------------- ---------- ---------- ----------
b??????????????????? 1????????? 5????????? 9
c??????????????????? 2????????? 6????????? 10
d??????????????????? 3????????? 7????????? 11
e??????????????????? 4????????? 8????????? 12

*/

--生成測試數(shù)據(jù)
create table test1(A varchar(20),b int,c int,d int,e int)
insert into test1 select 'x',1,2 ,3 ,4
insert into test1 select 'y',5,6 ,7 ,8
insert into test1 select 'z',9,10,11,12
go

--生成中間數(shù)據(jù)表
declare @s varchar(8000)
set @s = 'create table test2(a varchar(20)'
select @s = @s + ',' + A + ' varchar(10)' from test1
set @s = @s + ')'
exec(@s)
print @s
--借助中間表實現(xiàn)行列轉換
declare @name varchar(20)

declare t_cursor cursor for
select name from syscolumns
where id=object_id('test1') and colid > 1 order by colid

open t_cursor

fetch next from t_cursor into @name

while @@fetch_status = 0
begin
??? exec('select ' + @name + ' as t into test3 from test1')
??? set @s='insert into test2 select ''' + @name + ''''
??? select @s = @s + ',''' + rtrim(t) + '''' from test3
??? exec(@s)
??? exec('drop table test3')
??? fetch next from t_cursor into @name
end
close t_cursor
deallocate t_cursor

--查看行列互換處理結果
select * from test1
select * from test2

--刪除表
drop table test1
drop table test2
----------------------------------------------------------------------------
/*固定的寫法:*/
select t1.* , t2.y , t3.z from
(select a = 'b' , x = b from test1 where a = 'x') t1,
(select a = 'b' , y = b from test1 where a = 'y') t2,
(select a = 'b' , z = b from test1 where a = 'z') t3
where t1.a = t2.a and t1.a = t2.a
union all
select t1.* , t2.y , t3.z from
(select a = 'c' , x = c from test1 where a = 'x') t1,
(select a = 'c' , y = c from test1 where a = 'y') t2,
(select a = 'c' , z = c from test1 where a = 'z') t3
where t1.a = t2.a and t1.a = t2.a
union all
select t1.* , t2.y , t3.z from
(select a = 'd' , x = d from test1 where a = 'x') t1,
(select a = 'd' , y = d from test1 where a = 'y') t2,
(select a = 'd' , z = d from test1 where a = 'z') t3
where t1.a = t2.a and t1.a = t2.a
union all
select t1.* , t2.y , t3.z from
(select a = 'e' , x = e from test1 where a = 'x') t1,
(select a = 'e' , y = e from test1 where a = 'y') t2,
(select a = 'e' , z = e from test1 where a = 'z') t3
where t1.a = t2.a and t1.a = t2.a

----------------------------------------------------------------------------
/*
表tb,數(shù)據(jù)如下:
項目種類? 業(yè)績? 提成
洗吹類  200?? 10
外賣????? 100?? 5
合計????? 300?? 15
轉換成:
項目種類? 洗吹類? 外賣? 合計
業(yè)績????? 200???? 100?? 300
提成????? 10????? 5???? 15
*/

create table tb
(
? 項目種類 varchar(10),
? 業(yè)績???? int,
? 提成???? int
)

insert into tb(項目種類,業(yè)績,提成) values('洗吹類',200,10)
insert into tb(項目種類,業(yè)績,提成) values('外賣'? ,100,5)
insert into tb(項目種類,業(yè)績,提成) values('合計'? ,300,15)
go

select 項目種類,sum(洗吹類) as 洗吹類 , sum(外賣) as 外賣 , sum(合計) as 合計 from
(
? select 項目種類 = '業(yè)績',
???????? 洗吹類?? = case when 項目種類 = '洗吹類' then 業(yè)績 else 0 end,
???????? 外賣???? = case when 項目種類 = '外賣'?? then 業(yè)績 else 0 end,
???????? 合計???? = case when 項目種類 = '合計'?? then 業(yè)績 else 0 end
? from tb
union all
? select 項目種類 = '提成' ,
???????? 洗吹類?? = case when 項目種類 = '洗吹類' then 提成 else 0 end,
???????? 外賣???? = case when 項目種類 = '外賣'?? then 提成 else 0 end,
???????? 合計???? = case when 項目種類 = '合計'?? then 提成 else 0 end
? from tb
) m
group by 項目種類
order by 項目種類 desc

drop table tb

/*
項目種類 洗吹類????? 外賣??????? 合計?????????
-------- ----------- ----------- -----------
業(yè)績???? 200???????? 100???????? 300
提成???? 10????????? 5?????????? 15

(所影響的行數(shù)為 2 行)
*/

--------------------------------------------------------------------------
/*
數(shù)據(jù)庫中tb表格如下

月份??? 工資?? 福利? 獎金
1月???? 100??? 200?? 300
2月???? 110??? 210?? 310
3月???? 120??? 220?? 320
4月???? 130??? 230?? 330

我想得到的結果是

項目?? 1月??? 2月? 3月? 4月
工資?? 100??? 110? 120? 130
福利?? 200??? 210? 220? 230
獎金?? 300??? 310? 320? 330

就是說完全把表格的行列顛倒,有點像那種旋轉矩陣,請問如何用sql 語句實現(xiàn)?
*/

if exists (select * from dbo.sysobjects
where id = object_id(N'[dbo].[p_zj]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_zj]
GO
/*--行列互換的通用存儲過程(原著:鄒建):將指定的表,按指定的字段進行行列互換*/

create proc p_zj
?????? @tbname sysname, --要處理的表名
?????? @fdname sysname, --做為轉換的列名
?????? @new_fdname sysname='' --為轉換后的列指定列名
as
declare @s1 varchar(8000) , @s2 varchar(8000),
??????? @s3 varchar(8000) , @s4 varchar(8000),
??????? @s5 varchar(8000) , @i varchar(10)
select @s1 = '' , @s2 = '' , @s3 = '' , @s4 = '' , @s5 = '' , @i = '0'
select @s1 = @s1 + ',@' + @i + ' varchar(8000)',
?????? @s2 = @s2 + ',@' + @i + '=''' + case isnull(@new_fdname , '') when '' then ''
?????? else @new_fdname + '=' end + '''''' + name + '''''''',
?????? @s3 = @s3 + 'select @' + @i + '=@' + @i + '+'',['' + [' + @fdname +
?????? ']+'']=''+cast([' + name + '] as varchar) from [' + @tbname + ']',
?????? @s4 = @s4 + ',@' + @i + '=''select ''+@' + @i,
?????? @s5 = @s5 + '+'' union all ''+@' + @i,
?????? @i=cast(@i as int)+1
from syscolumns
where object_id(@tbname)=id and name<>@fdname

select @s1=substring(@s1,2,8000),
?????? @s2=substring(@s2,2,8000),
?????? @s4=substring(@s4,2,8000),
?????? @s5=substring(@s5,16,8000)
exec('declare ' + @s1 + 'select ' + @s2 + @s3 + 'select ' + @s4 + '
exec(' + @s5 + ')')
go

--用上面的存儲過程測試:

create table Test(月份 varchar(4), 工資 int, 福利 int, 獎金 int)
insert Test
select '1月',100,200,300 union all
select '2月',110,210,310 union all
select '3月',120,220,320 union all
select '4月',130,230,330
go

exec p_zj 'Test', '月份' , '項目'

drop table Test
drop proc p_zj

/*
項目?? 1月???????? 2月???????? 3月???????? 4月?????????
---- ----------- ----------- ----------- -----------
福利?? 200???????? 210???????? 220???????? 230
工資?? 100???????? 110???????? 120???????? 130
獎金?? 300???????? 310???????? 320???????? 330

(所影響的行數(shù)為 3 行)
*/

/*
靜態(tài)寫法(SQL2005)
*/
--測試環(huán)境
create table Test(月份 varchar(4), 工資 int, 福利 int, 獎金 int)
insert Test
select '1月',100,200,300 union all
select '2月',110,210,310 union all
select '3月',120,220,320 union all
select '4月',130,230,330
go
--測試語句
SELECT * FROM
(
? SELECT 考核月份,月份,金額 FROM
???? (SELECT 月份, 工資, 福利, 獎金 FROM Test) p
? UNPIVOT
???? (金額 FOR 考核月份 IN (工資, 福利, 獎金))AS unpvt
) T
PIVOT
(MAX(金額)? FOR 月份 in ([1月],[2月],[3月],[4月]))AS pt

--測試結果

/*
考核月份? 1月???? 2月????? 3月???? 4月
-------? -----? -----?? ------? -------
福利200210220230
工資100110120130
獎金300310320330
*/

--刪除環(huán)境
Drop table Test

本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或將催生出更大的獨角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉型技術解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關鍵字: 汽車 人工智能 智能驅動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務中斷的風險,如企業(yè)系統(tǒng)復雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務連續(xù)性,提升韌性,成...

關鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質量流程IT總裁陶景文發(fā)表了演講。

關鍵字: 華為 12nm EDA 半導體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權最終是由生態(tài)的繁榮決定的。

關鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實提質增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務引領增長 以科技創(chuàng)新為引領,提升企業(yè)核心競爭力 堅持高質量發(fā)展策略,塑強核心競爭優(yōu)勢...

關鍵字: 通信 BSP 電信運營商 數(shù)字經(jīng)濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術學會聯(lián)合牽頭組建的NVI技術創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術創(chuàng)新聯(lián)...

關鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關鍵字: BSP 信息技術
關閉
關閉