select max(case [week] when 0 then rq else 0 end) as [0] ,max(case [week] when 1 then rq else 0 end) as [1] ,max(case [week] when 2 then rq else 0 end) as [2] ,max(case [week] when 3 then rq else 0 end) as [3] ,max(case [week] when 4 then rq else 0 end) as [4] ,max(case [week] when 5 then rq else 0 end) as [5] ,max(case [week] when 6 then rq else 0 end) as [6] from (select *,(select count(1) from tab where [week] = a.week and rq<= a.rq) as id from tab a) as t group by id order by id
--行列互转 /****************************************************************************************************************************************************** 以学生成绩为例子,比较形象易懂 ******************************************************************************************************************************************************/--1、行互列 --> --> (Roy)生成測試數據if not object_id('Class') is 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] 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] 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), [总成绩]=sum([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、列转行 --> --> (Roy)生成測試數據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(8 行受影响) */
select max(case [week] when 0 then rq else 0 end) as [0] ,max(case [week] when 1 then rq else 0 end) as [1] ,max(case [week] when 2 then rq else 0 end) as [2] ,max(case [week] when 3 then rq else 0 end) as [3] ,max(case [week] when 4 then rq else 0 end) as [4] ,max(case [week] when 5 then rq else 0 end) as [5] ,max(case [week] when 6 then rq else 0 end) as [6] from tb
select max(case week when 0 then rq end) [0], max(case week when 1 then rq end) [1], max(case week when 2 then rq end) [2], max(case week when 3 then rq end) [3], max(case week when 4 then rq end) [4], max(case week when 5 then rq end) [5], max(case week when 6 then rq end) [6] from ( select * , px = (select count(1) from tb where week = t.week and rq < t.rq) + 1 from tb t ) t group by px group by
根据月份得出日历,求一sql 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 像这样的 ---------------------------------------declare @month as varchar(7) set @month = '2007-12'select 日,一,二,三,四,五,六 from ( select week , max(case weekday when 1 then datename(day,dt) else '' end ) '日', max(case weekday when 2 then datename(day,dt) else '' end ) '一', max(case weekday when 3 then datename(day,dt) else '' end ) '二', max(case weekday when 4 then datename(day,dt) else '' end ) '三', max(case weekday when 5 then datename(day,dt) else '' end ) '四', max(case weekday when 6 then datename(day,dt) else '' end ) '五', max(case weekday when 7 then datename(day,dt) else '' end ) '六' from ( select week = datepart(week , m.dt) , weekday = datepart(weekday , m.dt) , dt from ( select dt = @month + '-' + right('00'+cast(t.id as varchar),2) from ( select 1 as id union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10 union select 11 union select 12 union select 13 union select 14 union select 15 union select 16 union select 17 union select 18 union select 19 union select 20 union select 21 union select 22 union select 23 union select 24 union select 25 union select 26 union select 27 union select 28 union select 29 union select 30 union select 31 ) t where isdate(@month + '-' + right('00'+cast(t.id as varchar),2)) = 1 and @month + '-' + right('00'+cast(t.id as varchar),2) <= dateadd(month , 1 , @month + '-01') ) m ) n group by week ) o /* 日 一 二 三 四 五 六 -- -- -- -- -- -- -- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 (所影响的行数为 6 行) */ ---------------------------------------------------------------------- 用函数解决。(libin_ftsafe) create function f_calendar(@year int,@month int) returns @t table(日 varchar(4),一 varchar(4),二 varchar(4),三 varchar(4),四 varchar(4),五 varchar(4),六 varchar(4)) as begin declare @a table(id int identity(0,1),date datetime)
insert into @a(date) select top 31 rtrim(@year)+'-'+rtrim(@month)+'-1' from sysobjects
update @a set date=dateadd(dd,id,date) insert into @t select max(case datepart(dw,date) when 7 then rtrim(day(date)) else '' end), max(case datepart(dw,date) when 1 then rtrim(day(date)) else '' end), max(case datepart(dw,date) when 2 then rtrim(day(date)) else '' end), max(case datepart(dw,date) when 3 then rtrim(day(date)) else '' end), max(case datepart(dw,date) when 4 then rtrim(day(date)) else '' end), max(case datepart(dw,date) when 5 then rtrim(day(date)) else '' end), max(case datepart(dw,date) when 6 then rtrim(day(date)) else '' end) from @a where month(date)=@month group by (case datepart(dw,date) when 7 then datepart(week,date)+1 else datepart(week,date) end) return end goset datefirst 1 select * from dbo.f_calendar(2007,12) /* 日 一 二 三 四 五 六 ---- ---- ---- ---- ---- ---- ---- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 */ godrop function f_calendar go
max(case [week] when 0 then rq else 0 end) as [0]
,max(case [week] when 1 then rq else 0 end) as [1]
,max(case [week] when 2 then rq else 0 end) as [2]
,max(case [week] when 3 then rq else 0 end) as [3]
,max(case [week] when 4 then rq else 0 end) as [4]
,max(case [week] when 5 then rq else 0 end) as [5]
,max(case [week] when 6 then rq else 0 end) as [6]
from (select *,(select count(1) from tab where [week] = a.week and rq<= a.rq) as id
from tab a) as t
group by id
order by id
--行列互转
/******************************************************************************************************************************************************
以学生成绩为例子,比较形象易懂
******************************************************************************************************************************************************/--1、行互列
--> --> (Roy)生成測試數據if not object_id('Class') is 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]
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]
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),
[总成绩]=sum([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、列转行
--> --> (Roy)生成測試數據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(8 行受影响)
*/
max(case [week] when 0 then rq else 0 end) as [0]
,max(case [week] when 1 then rq else 0 end) as [1]
,max(case [week] when 2 then rq else 0 end) as [2]
,max(case [week] when 3 then rq else 0 end) as [3]
,max(case [week] when 4 then rq else 0 end) as [4]
,max(case [week] when 5 then rq else 0 end) as [5]
,max(case [week] when 6 then rq else 0 end) as [6]
from tb
04 05 06 07 01 02 03
11 12 13 14 08 09 10
18 19 20 21 15 16 17
25 26 27 28 22 23 24
NULL NULL NULL NULL 29 30 31
这种情况能不能改成:0 1 2 3 4 5 6
NULL NULL NULL NULL 01 02 03
04 05 06 07 08 09 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
max(case week when 0 then rq end) [0],
max(case week when 1 then rq end) [1],
max(case week when 2 then rq end) [2],
max(case week when 3 then rq end) [3],
max(case week when 4 then rq end) [4],
max(case week when 5 then rq end) [5],
max(case week when 6 then rq end) [6]
from
(
select * , px = (select count(1) from tb where week = t.week and rq < t.rq) + 1 from tb t
) t
group by px
group by
04 05 06 07 01 02 03
11 12 13 14 08 09 10
18 19 20 21 15 16 17
25 26 27 28 22 23 24
NULL NULL NULL NULL 29 30 31
这种情况能不能改成: 0 1 2 3 4 5 6
NULL NULL NULL NULL 01 02 03
04 05 06 07 08 09 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
[/Quote]
日 一 二 三 四 五 六
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 像这样的
---------------------------------------declare @month as varchar(7)
set @month = '2007-12'select 日,一,二,三,四,五,六 from
(
select week ,
max(case weekday when 1 then datename(day,dt) else '' end ) '日',
max(case weekday when 2 then datename(day,dt) else '' end ) '一',
max(case weekday when 3 then datename(day,dt) else '' end ) '二',
max(case weekday when 4 then datename(day,dt) else '' end ) '三',
max(case weekday when 5 then datename(day,dt) else '' end ) '四',
max(case weekday when 6 then datename(day,dt) else '' end ) '五',
max(case weekday when 7 then datename(day,dt) else '' end ) '六'
from
(
select week = datepart(week , m.dt) , weekday = datepart(weekday , m.dt) , dt from
(
select dt = @month + '-' + right('00'+cast(t.id as varchar),2) from
(
select 1 as id union select 2 union select 3 union select 4 union select 5
union select 6 union select 7 union select 8 union select 9 union select 10
union select 11 union select 12 union select 13 union select 14 union select 15
union select 16 union select 17 union select 18 union select 19 union select 20
union select 21 union select 22 union select 23 union select 24 union select 25
union select 26 union select 27 union select 28 union select 29 union select 30
union select 31
) t
where isdate(@month + '-' + right('00'+cast(t.id as varchar),2)) = 1 and @month + '-' + right('00'+cast(t.id as varchar),2) <= dateadd(month , 1 , @month + '-01')
) m
) n
group by week
) o
/*
日 一 二 三 四 五 六
-- -- -- -- -- -- --
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 (所影响的行数为 6 行)
*/
----------------------------------------------------------------------
用函数解决。(libin_ftsafe)
create function f_calendar(@year int,@month int)
returns @t table(日 varchar(4),一 varchar(4),二 varchar(4),三 varchar(4),四 varchar(4),五 varchar(4),六 varchar(4))
as
begin declare @a table(id int identity(0,1),date datetime)
insert into @a(date)
select top 31 rtrim(@year)+'-'+rtrim(@month)+'-1' from sysobjects
update @a set date=dateadd(dd,id,date) insert into @t
select
max(case datepart(dw,date) when 7 then rtrim(day(date)) else '' end),
max(case datepart(dw,date) when 1 then rtrim(day(date)) else '' end),
max(case datepart(dw,date) when 2 then rtrim(day(date)) else '' end),
max(case datepart(dw,date) when 3 then rtrim(day(date)) else '' end),
max(case datepart(dw,date) when 4 then rtrim(day(date)) else '' end),
max(case datepart(dw,date) when 5 then rtrim(day(date)) else '' end),
max(case datepart(dw,date) when 6 then rtrim(day(date)) else '' end)
from
@a
where
month(date)=@month
group by
(case datepart(dw,date) when 7 then datepart(week,date)+1 else datepart(week,date) end) return
end
goset datefirst 1
select * from dbo.f_calendar(2007,12)
/*
日 一 二 三 四 五 六
---- ---- ---- ---- ---- ---- ----
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
*/
godrop function f_calendar
go