select max(case when khbh=1 then khxm else '' end) as khxm1, max(case when khbh=2 then khxm else '' end) as khxm2 from kh 列变行转换后: khxm1 khxm2 张三 李四
if object_id('tb') is not null drop table tb go create table tb(姓名 varchar(6),日期 date,日期类型 varchar(6),时间段 varchar(50),小时数 numeric(5,1),加班理由 varchar(6)) insert into tb select '张三', '2010-08-01', '周末', '8:30:00~20:30:00', 12 ,'发货' union all select '张三', '2010-08-02', '平日', '18:30:00~20:30:00', 2 ,'收货' union all select '张三', '2010-08-03', '平日', '18:00:00~20:30:00', 2.5, '验货' union all select '张三', '2010-08-04', '平日', '17:30:00~20:30:00', 3 ,'配货' union all select '张三', '2010-08-28', '周末', '8:30:00~17:30:00', 9 , null union all select '张三', '2010-08-29', '周末', '18:30:00~20:30:00', 2 ,'收货' union all select '张三', '2010-08-30', '平日', '18:00:00~20:30:00', 2.5, '验货' union all select '张三', '2010-08-31', '平日', '17:30:00~20:30:00', 3 ,'配货' union all select '李四', '2010-08-01', '周末', '8:30:00~20:30:00', 12 ,'发货' union all select '李四', '2010-08-02', '平日', '18:30:00~20:30:00', 2 ,'收货' union all select '李四', '2010-08-03', '平日', '18:00:00~20:30:00', 2.5, '验货' union all select '李四', '2010-08-04', '平日', '17:30:00~20:30:00', 3 ,'配货' union all select '李四', '2010-08-28', '周末', '8:30:00~17:30:00', 9 , null union all select '李四', '2010-08-29', '周末', '18:30:00~20:30:00', 2, '收货' union all select '李四', '2010-08-30', '平日', '18:00:00~20:30:00', 2.5,' 验货' union all select '李四', '2010-08-31', '平日', '17:30:00~20:30:00', 3 ,'配货'declare @s1 nvarchar(max), @s2 nvarchar(max), @s3 nvarchar(max) set @s1='' set @s2='' set @s3='' select @s1=@s1+','''+ltrim(日期)+日期类型+''' = max(case 日期 when '''+ltrim(日期)+''' then 时间段 else null end)' from tb group by 日期,日期类型 select @s2=@s2+','''+ltrim(日期)+日期类型+''' = max(case 日期 when '''+ltrim(日期)+''' then ltrim(小时数) else null end)' from tb group by 日期,日期类型 select @s3=@s3+','''+ltrim(日期)+日期类型+''' = max(case 日期 when '''+ltrim(日期)+''' then 加班理由 else null end)' from tb group by 日期,日期类型 exec('select * from ( select ltrim(姓名) as 姓名 '+@s1+',0 as 平日汇总,0 as 周末汇总 from tb group by 姓名 union all select 姓名+''小时数'' '+@s2+', (select sum(小时数) from tb where a.姓名=姓名 and 日期类型=''平日'') , (select sum(小时数) from tb where a.姓名=姓名 and 日期类型=''周末'') from tb a group by 姓名 union all select 姓名+''加班理由'' '+@s3+',0 as 平日汇总,0 as 周末汇总 from tb group by 姓名 ) a order by 姓名 ') 姓名 2010-08-01周末 2010-08-02平日 2010-08-03平日 2010-08-04平日 2010-08-28周末 2010-08-29周末 2010-08-30平日 2010-08-31平日 平日汇总 周末汇总 李四 8:30:00~20:30:00 18:30:00~20:30:00 18:00:00~20:30:00 17:30:00~20:30:00 8:30:00~17:30:00 18:30:00~20:30:00 18:00:00~20:30:00 17:30:00~20:30:00 0.0 0.0 李四加班理由 发货 收货 验货 配货 NULL 收货 验货 配货 0.0 0.0 李四小时数 12.0 2.0 2.5 3.0 9.0 2.0 2.5 3.0 13.0 23.0 张三 8:30:00~20:30:00 18:30:00~20:30:00 18:00:00~20:30:00 17:30:00~20:30:00 8:30:00~17:30:00 18:30:00~20:30:00 18:00:00~20:30:00 17:30:00~20:30:00 0.0 0.0 张三加班理由 发货 收货 验货 配货 NULL 收货 验货 配货 0.0 0.0 张三小时数 12.0 2.0 2.5 3.0 9.0 2.0 2.5 3.0 13.0 23.0
khbh khxm
1 张三
2 李四
SQL列变行转换:
select
max(case when khbh=1 then khxm else '' end) as khxm1,
max(case when khbh=2 then khxm else '' end) as khxm2
from kh
列变行转换后:
khxm1 khxm2
张三 李四
if object_id('tb') is not null
drop table tb
go
create table tb(姓名 varchar(6),日期 date,日期类型 varchar(6),时间段 varchar(50),小时数 numeric(5,1),加班理由 varchar(6))
insert into tb
select '张三', '2010-08-01', '周末', '8:30:00~20:30:00', 12 ,'发货' union all
select '张三', '2010-08-02', '平日', '18:30:00~20:30:00', 2 ,'收货' union all
select '张三', '2010-08-03', '平日', '18:00:00~20:30:00', 2.5, '验货' union all
select '张三', '2010-08-04', '平日', '17:30:00~20:30:00', 3 ,'配货' union all
select '张三', '2010-08-28', '周末', '8:30:00~17:30:00', 9 , null union all
select '张三', '2010-08-29', '周末', '18:30:00~20:30:00', 2 ,'收货' union all
select '张三', '2010-08-30', '平日', '18:00:00~20:30:00', 2.5, '验货' union all
select '张三', '2010-08-31', '平日', '17:30:00~20:30:00', 3 ,'配货' union all
select '李四', '2010-08-01', '周末', '8:30:00~20:30:00', 12 ,'发货' union all
select '李四', '2010-08-02', '平日', '18:30:00~20:30:00', 2 ,'收货' union all
select '李四', '2010-08-03', '平日', '18:00:00~20:30:00', 2.5, '验货' union all
select '李四', '2010-08-04', '平日', '17:30:00~20:30:00', 3 ,'配货' union all
select '李四', '2010-08-28', '周末', '8:30:00~17:30:00', 9 , null union all
select '李四', '2010-08-29', '周末', '18:30:00~20:30:00', 2, '收货' union all
select '李四', '2010-08-30', '平日', '18:00:00~20:30:00', 2.5,' 验货' union all
select '李四', '2010-08-31', '平日', '17:30:00~20:30:00', 3 ,'配货'declare @s1 nvarchar(max),
@s2 nvarchar(max),
@s3 nvarchar(max)
set @s1=''
set @s2=''
set @s3=''
select @s1=@s1+','''+ltrim(日期)+日期类型+''' = max(case 日期 when '''+ltrim(日期)+''' then 时间段 else null end)'
from tb
group by 日期,日期类型
select @s2=@s2+','''+ltrim(日期)+日期类型+''' = max(case 日期 when '''+ltrim(日期)+''' then ltrim(小时数) else null end)'
from tb
group by 日期,日期类型
select @s3=@s3+','''+ltrim(日期)+日期类型+''' = max(case 日期 when '''+ltrim(日期)+''' then 加班理由 else null end)'
from tb
group by 日期,日期类型
exec('select * from
(
select ltrim(姓名) as 姓名 '+@s1+',0 as 平日汇总,0 as 周末汇总 from tb group by 姓名
union all
select 姓名+''小时数'' '+@s2+', (select sum(小时数) from tb where a.姓名=姓名 and 日期类型=''平日'')
, (select sum(小时数) from tb where a.姓名=姓名 and 日期类型=''周末'')
from tb a group by 姓名
union all
select 姓名+''加班理由'' '+@s3+',0 as 平日汇总,0 as 周末汇总 from tb group by 姓名
) a
order by 姓名 ')
姓名 2010-08-01周末 2010-08-02平日 2010-08-03平日 2010-08-04平日 2010-08-28周末 2010-08-29周末 2010-08-30平日 2010-08-31平日 平日汇总 周末汇总
李四 8:30:00~20:30:00 18:30:00~20:30:00 18:00:00~20:30:00 17:30:00~20:30:00 8:30:00~17:30:00 18:30:00~20:30:00 18:00:00~20:30:00 17:30:00~20:30:00 0.0 0.0
李四加班理由 发货 收货 验货 配货 NULL 收货 验货 配货 0.0 0.0
李四小时数 12.0 2.0 2.5 3.0 9.0 2.0 2.5 3.0 13.0 23.0
张三 8:30:00~20:30:00 18:30:00~20:30:00 18:00:00~20:30:00 17:30:00~20:30:00 8:30:00~17:30:00 18:30:00~20:30:00 18:00:00~20:30:00 17:30:00~20:30:00 0.0 0.0
张三加班理由 发货 收货 验货 配货 NULL 收货 验货 配货 0.0 0.0
张三小时数 12.0 2.0 2.5 3.0 9.0 2.0 2.5 3.0 13.0 23.0