34,576
社区成员
发帖
与我相关
我的任务
分享
--原来格式(注意:结果只有1行):
ClassMarker ClassID tTime
H 1 2012-03-13
--我想实现如下格式:
ClassMarker H
ClassID 1
tTime 2012-03-13
我读取数据的SQL语句如下,
select top 1 ClassMarker,ClassID,tTime from CTable where ClassID = '1'
--类型转换一下就ok了
select top 1 'ClassMarker', ltrim(ClassMarker) from CTable where ClassID = '1'
union all
select top 1 'ClassID', ltrim(ClassID) from CTable where ClassID = '1'
union all
select top 1 'tTime', convert(varchar(20),tTime,120) from CTable where ClassID = '1'
--ClassMarker ClassID tTime
--H 1 2012-03-13
--
----我想实现如下格式:
--
--ClassMarker H
--ClassID 1
--tTime 2012-03-13
--
--我读取数据的SQL语句如下,
if object_id('CTable') is not null
drop table CTable
go
create table CTable (ClassMarker varchar(10),ClassID varchar(10),tTime varchar(20))
insert into CTable values('H', 1,'2012-03-13')
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#t') and type='U')
drop table #t
go
select id=identity(int ,1,1),* into #t from
(select top 1 ClassMarker from CTable where ClassID = '1'
union all
select top 1 ClassID as ClassMarker from CTable where ClassID = '1'
union all
select top 1 tTime as ClassMarker from CTable where ClassID = '1'
)a
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#g') and type='U')
drop table #g
go
select id=identity(int ,1,1),* into #g from
(SELECT name FROM syscolumns WHERE (id =(SELECT id FROM sysobjects WHERE name = 'CTable'))) e
select g.name,t.classmarker from #t t join #g g on t.id=g.id
name classmarker
-------------------------------------------------------------------------------------------------------------------------------- --------------------
ClassMarker H
ClassID 1
tTime 2012-03-13
(3 row(s) affected)
--ClassMarker ClassID tTime
--H 1 2012-03-13
--
----我想实现如下格式:
--
--ClassMarker H
--ClassID 1
--tTime 2012-03-13
--
--我读取数据的SQL语句如下,
if object_id('CTable') is not null
drop table CTable
go
create table CTable (ClassMarker varchar(10),ClassID varchar(10),tTime varchar(20))
insert into CTable values('H', 1,'2012-03-13')
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#t') and type='U')
drop table #t
go
select id=identity(int ,1,1),* into #t from
(select top 1 ClassMarker from CTable where ClassID = '1'
union all
select top 1 ClassID as ClassMarker from CTable where ClassID = '1'
union all
select top 1 tTime as ClassMarker from CTable where ClassID = '1'
)a
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#g') and type='U')
drop table #g
go
select id=identity(int ,1,1),* into #g from
(SELECT name FROM syscolumns WHERE (id =(SELECT id FROM sysobjects WHERE name = 'CTable'))) e
select g.name,t.classmarker from #t t join #g g on t.id=g.id
name classmarker
-------------------------------------------------------------------------------------------------------------------------------- --------------------
ClassMarker H
ClassID 1
tTime 2012-03-13
(3 row(s) affected)
select item = 'ClassMarker' , value = cast(ClassMarker as varchar) from tb
union all
select item = 'ClassID' , value = cast(ClassID as varchar) from tb
union all
select item = 'tTime' , value = convert(varchar(10),tTime,120) from tb
/*--行列互换的通用存储过程(原著:邹建):将指定的表,按指定的字段进行行列互换*/
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月
-------- ------ -------- -------- --------
奖金 300 310 320 330
工资 100 110 120 130
福利 200 210 220 230
(所影响的行数为 3 行)
*/
--SQL2005静态写法
--创建测试数据
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
drop table test
/*
项目 1月 2月 3月 4月
-------- ------ -------- -------- --------
奖金 300 310 320 330
工资 100 110 120 130
福利 200 210 220 230
(3 行受影响)
*/
--行列互转
--1、行换列
if object_id('Class') is not null
drop table Class
Go
Create table Class(
[Student] nvarchar(2),
[Course] nvarchar(2),
[Score] int)
Insert Class
select N'张三',N'语文',78 union all
select N'张三',N'数学',87 union all
select N'张三',N'英语',82 union all
select N'张三',N'物理',90 union all
select N'李四',N'语文',65 union all
select N'李四',N'数学',77 union all
select N'李四',N'英语',65 union all
select N'李四',N'物理',85
Go
--2000方法:
--动态:
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([Course])+'=max(case when [Course]='
+quotename([Course],'''')+' then [Score] else 0 end)'
from
Class group by[Course]
--select @s
exec('select [Student]'+@s+' from Class group by [Student]')
--生成静态:
select
[Student],
[数学]=max(case when [Course]='数学' then [Score] else 0 end),
[物理]=max(case when [Course]='物理' then [Score] else 0 end),
[英语]=max(case when [Course]='英语' then [Score] else 0 end),
[语文]=max(case when [Course]='语文' then [Score] else 0 end)
from
Class
group by [Student]
GO
--动态:
declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course]
select @s
exec('select * from Class pivot (max([Score]) for [Course] in('+@s+'))b')
--生成静态:
select *
from
Class
pivot
(max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b
--生成格式:
/*
Student 数学 物理 英语 语文
------- ----------- ----------- ----------- -----------
李四 77 85 65 65
张三 87 90 82 78
(2 行受影响)
*/
go
--加上总成绩(学科平均分)
--2000方法:
--动态:
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+'then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+',[总成绩]=sum([Score]) from Class group by [Student]')--加多一列(学科平均分用avg([Score]))
生成动态:
select
[Student],
[数学]=max(case when [Course]='数学' then [Score] else 0 end),
[物理]=max(case when [Course]='物理' then [Score] else 0 end),
[英语]=max(case when [Course]='英语' then [Score] else 0 end),
[语文]=max(case when [Course]='语文' then [Score] else 0 end),
[总成绩]=([Score]) --加多一列(学科平均分用avg([Score]))
from
Class
group by [Student]
go
--2005方法:
动态:
declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course]
--isnull(@s+',','') 去掉字符串@s中第一个逗号
exec('select [Student],'+@s+',[总成绩] from (select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a
pivot (max([Score]) for [Course] in('+@s+'))b ')
--生成静态:
select
[Student],[数学],[物理],[英语],[语文],[总成绩]
from
(select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a --平均分时用avg([Score])
pivot
(max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b
生成格式:
/*
Student 数学 物理 英语 语文 总成绩
------- ----------- ----------- ----------- ----------- -----------
李四 77 85 65 65 292
张三 87 90 82 78 337
(2 行受影响)
*/
go
--2、列转行
if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[数学] int,[物理] int,[英语] int,[语文] int)
Insert Class
select N'李四',77,85,65,65 union all
select N'张三',87,90,82,78
Go
--2000:
动态:
declare @s nvarchar(4000)
select @s=isnull(@s+' union all ','')+'select [Student],[Course]='+quotename(Name,'''')--isnull(@s+' union all ','') 去掉字符串@s中第一个union all
+',[Score]='+quotename(Name)+' from Class'
from syscolumns where ID=object_id('Class') and Name not in('Student')--排除不转换的列
order by Colid
exec('select * from ('+@s+')t order by [Student],[Course]')--增加一个排序
生成静态:
select *
from (select [Student],[Course]='数学',[Score]=[数学] from Class union all
select [Student],[Course]='物理',[Score]=[物理] from Class union all
select [Student],[Course]='英语',[Score]=[英语] from Class union all
select [Student],[Course]='语文',[Score]=[语文] from Class)t
order by [Student],[Course]
go
--2005:
动态:
declare @s nvarchar(4000)
select @s=isnull(@s+',','')+quotename(Name)
from syscolumns where ID=object_id('Class') and Name not in('Student')
order by Colid
exec('select Student,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')
go
select
Student,[Course],[Score]
from
Class
unpivot
([Score] for [Course] in([数学],[物理],[英语],[语文]))b
生成格式:
/*
Student Course Score
------- ------- -----------
李四 数学 77
李四 物理 85
李四 英语 65
李四 语文 65
张三 数学 87
张三 物理 90
张三 英语 82
张三 语文 78
*/
select top 1 'ClassMarker', ClassMarker from CTable where ClassID = '1'
union all
select top 1 'ClassID', ClassID from CTable where ClassID = '1'
union all
select top 1 'tTime', tTime from CTable where ClassID = '1'