日期:2014-05-17 浏览次数:20651 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (name nvarchar(2),sell_ID int,kioskID nvarchar(8),classes nvarchar(4),time_from datetime,time_to datetime)
insert into [TB]
select '柳',1001,'一号票厅','上午','2013/4/5','2013/4/15' union all
select '军',1002,'二号票厅','下午','2013/4/5','2013/4/15' union all
select '贞',1003,'一号票厅','下午','2013/4/3','2013/4/13' union all
select '霞',1004,'六号票厅','上午','2013/4/12','2013/4/20'
select * from [TB]
;WITH tt
AS(
SELECT classes,time_from,time_to,
MAX(CASE WHEN kioskid='一号票厅' THEN NAME ELSE '' END ) AS '一号票厅',
MAX(CASE WHEN kioskid='二号票厅' THEN NAME ELSE '' END ) AS '二号票厅',
MAX(CASE WHEN kioskid='三号票厅' THEN NAME ELSE '' END ) AS '三号票厅',
MAX(CASE WHEN kioskid='四号票厅' THEN NAME ELSE '' END ) AS '四号票厅',
MAX(CASE WHEN kioskid='五号票厅' THEN NAME ELSE '' END ) AS '五号票厅',
MAX(CASE WHEN kioskid='六号票厅' THEN NAME ELSE '' END ) AS '六号票厅'
FROM dbo.TB
GROUP BY classes,time_from,time_to)
SELECT DATEADD(dd, m.number, tt.time_from) AS 日期 ,
tt.classes ,
tt.一号票厅 ,
tt.二号票厅 ,
tt.三号票厅 ,
tt.四号票厅 ,
tt.五号票厅 ,
tt.六号票厅
FROM tt
LEFT JOIN master..spt_values M ON DATEADD(dd, m.number, tt.time_from) BETWEEN tt.time_from
AND
tt.time_to