日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

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

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

問題:假設(shè)有張學生成績表(tb)如下:
姓名 課程 分數(shù)
張三 語文 74
張三 數(shù)學 83
張三 物理 93
李四 語文 74
李四 數(shù)學 84
李四 物理 94
想變成(得到如下結(jié)果):
姓名 語文 數(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')

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

/*
問題:在上述結(jié)果的基礎(chǔ)上加平均分,總分,得到如下結(jié)果:
姓名 語文 數(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???

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

/*
問題:如果上述兩表互相換一下:即表結(jié)構(gòu)和數(shù)據(jù)為:
姓名 語文 數(shù)學 物理
張三 74  83  93
李四 74  84  94
想變成(得到如下結(jié)果):
姓名 課程 分數(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。
--調(diào)用系統(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。

--------------------
/*
問題:在上述的結(jié)果上加個平均分,總分,得到如下結(jié)果:
姓名 課程?? 分數(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):
---調(diào)用系統(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ù)旋轉(zhuǎn)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

轉(zhuǎn)化成如下結(jié)果:
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)行列轉(zhuǎ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

--查看行列互換處理結(jié)果
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
轉(zhuǎn)換成:
項目種類? 洗吹類? 外賣? 合計
業(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

我想得到的結(jié)果是

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

就是說完全把表格的行列顛倒,有點像那種旋轉(zhuǎn)矩陣,請問如何用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, --做為轉(zhuǎn)換的列名
?????? @new_fdname sysname='' --為轉(zhuǎn)換后的列指定列名
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

--測試結(jié)果

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

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

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

LED驅(qū)動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: 驅(qū)動電源

在工業(yè)自動化蓬勃發(fā)展的當下,工業(yè)電機作為核心動力設(shè)備,其驅(qū)動電源的性能直接關(guān)系到整個系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動勢抑制與過流保護是驅(qū)動電源設(shè)計中至關(guān)重要的兩個環(huán)節(jié),集成化方案的設(shè)計成為提升電機驅(qū)動性能的關(guān)鍵。

關(guān)鍵字: 工業(yè)電機 驅(qū)動電源

LED 驅(qū)動電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個照明設(shè)備的使用壽命。然而,在實際應(yīng)用中,LED 驅(qū)動電源易損壞的問題卻十分常見,不僅增加了維護成本,還影響了用戶體驗。要解決這一問題,需從設(shè)計、生...

關(guān)鍵字: 驅(qū)動電源 照明系統(tǒng) 散熱

根據(jù)LED驅(qū)動電源的公式,電感內(nèi)電流波動大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關(guān)鍵字: LED 設(shè)計 驅(qū)動電源

電動汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產(chǎn)業(yè)的重要發(fā)展方向。電動汽車的核心技術(shù)之一是電機驅(qū)動控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機驅(qū)動系統(tǒng)中的關(guān)鍵元件,其性能直接影響到電動汽車的動力性能和...

關(guān)鍵字: 電動汽車 新能源 驅(qū)動電源

在現(xiàn)代城市建設(shè)中,街道及停車場照明作為基礎(chǔ)設(shè)施的重要組成部分,其質(zhì)量和效率直接關(guān)系到城市的公共安全、居民生活質(zhì)量和能源利用效率。隨著科技的進步,高亮度白光發(fā)光二極管(LED)因其獨特的優(yōu)勢逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關(guān)鍵字: 發(fā)光二極管 驅(qū)動電源 LED

LED通用照明設(shè)計工程師會遇到許多挑戰(zhàn),如功率密度、功率因數(shù)校正(PFC)、空間受限和可靠性等。

關(guān)鍵字: LED 驅(qū)動電源 功率因數(shù)校正

在LED照明技術(shù)日益普及的今天,LED驅(qū)動電源的電磁干擾(EMI)問題成為了一個不可忽視的挑戰(zhàn)。電磁干擾不僅會影響LED燈具的正常工作,還可能對周圍電子設(shè)備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來解決L...

關(guān)鍵字: LED照明技術(shù) 電磁干擾 驅(qū)動電源

開關(guān)電源具有效率高的特性,而且開關(guān)電源的變壓器體積比串聯(lián)穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機重量也有所下降,所以,現(xiàn)在的LED驅(qū)動電源

關(guān)鍵字: LED 驅(qū)動電源 開關(guān)電源

LED驅(qū)動電源是把電源供應(yīng)轉(zhuǎn)換為特定的電壓電流以驅(qū)動LED發(fā)光的電壓轉(zhuǎn)換器,通常情況下:LED驅(qū)動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: LED 隧道燈 驅(qū)動電源
關(guān)閉