日期:2014-05-17 浏览次数:20532 次
--> 测试数据:[tb] IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb] GO CREATE TABLE [tb]([ID] INT,[JOUR] DATETIME,[Week] VARCHAR(6)) INSERT [tb] SELECT 248,'2012-05-21','星期一' UNION ALL SELECT 249,'2012-05-22','星期二' UNION ALL SELECT 250,'2012-05-23','星期三' UNION ALL SELECT 251,'2012-05-24','星期四' UNION ALL SELECT 252,'2012-05-25','星期五' UNION ALL SELECT 253,'2012-05-26','星期六' UNION ALL SELECT 254,'2012-05-27','星期天' UNION ALL SELECT 255,'2012-05-28','星期一' UNION ALL SELECT 256,'2012-05-29','星期二' UNION ALL SELECT 257,'2012-05-30','星期三' UNION ALL SELECT 258,'2012-05-31','星期四' UNION ALL SELECT 259,'2012-06-01','星期五' UNION ALL SELECT 260,'2012-06-02','星期六' UNION ALL SELECT 261,'2012-06-03','星期天' UNION ALL SELECT 262,'2012-06-04','星期一' UNION ALL SELECT 263,'2012-06-05','星期二' UNION ALL SELECT 264,'2012-06-06','星期三' UNION ALL SELECT 265,'2012-06-07','星期四' UNION ALL SELECT 266,'2012-06-08','星期五' UNION ALL SELECT 267,'2012-06-09','星期六' GO --> 测试语句: select max(case when Week='星期一' then convert(varchar(10),JOUR,120) else '' end) as 星期一, max(case when Week='星期二' then convert(varchar(10),JOUR,120) else '' end) as 星期二, max(case when Week='星期三' then convert(varchar(10),JOUR,120) else '' end) as 星期三, max(case when Week='星期四' then convert(varchar(10),JOUR,120) else '' end) as 星期四, max(case when Week='星期五' then convert(varchar(10),JOUR,120) else '' end) as 星期五, max(case when Week='星期六' then convert(varchar(10),JOUR,120) else '' end) as 星期六, max(case when Week='星期天' then convert(varchar(10),JOUR,120) else '' end) as 星期天 from (select *,row_id=row_number() over(partition by Week order by [JOUR]) from [tb]) as t group by row_id /* 星期一 星期二 星期三 星期四 星期五 星期六 星期天 ---------- ---------- ---------- ---------- ---------- ---------- ---------- 2012-05-21 2012-05-22 2012-05-23 2012-05-24 2012-05-25 2012-05-26 2012-05-27 2012-05-28 2012-05-29 2012-05-30 2012-05-31 2012-06-01 2012-06-02 2012-06-03 2012-06-04 2012-06-05 2012-06-06 2012-06-07 2012-06-08 2012-06-09 (3 行受影响) */