27,579
社区成员
发帖
与我相关
我的任务
分享
create table a1(id int, name varchar(10))
insert into a1 values(1001 , 'a')
insert into a1 values(1002 , 'b')
insert into a1 values(1003 , 'c')
create table a2(id int, time varchar(10) , date varchar(10))
insert into a2 values(1001 , '08:12','2009-01-01')
insert into a2 values(1001 , '15:33','2009-01-01')
insert into a2 values(1002 , '08:11','2009-01-01')
insert into a2 values(1003 , '08:44','2009-01-01')
insert into a2 values(1002 , '18:22','2009-01-01')
insert into a2 values(1001 , '08:12','2009-01-02')
insert into a2 values(1001 , '15:33','2009-01-02')
insert into a2 values(1002 , '08:11','2009-01-02')
insert into a2 values(1003 , '08:44','2009-01-02')
insert into a2 values(1002 , '18:22','2009-01-02')
go
declare @sql varchar(8000)
set @sql = 'select id , name '
select @sql = @sql + ' , max(case convert(varchar(10),date,120) when ''' + date + ''' then time1 end) [' + date + '_time1]'
+ ' , max(case convert(varchar(10),date,120) when ''' + date + ''' then time2 end) [' + date + '_time2]'
from (select distinct convert(varchar(10),date,120) date from a2 where convert(varchar(7),date,120) = '2009-01') as a
set @sql = @sql + ' from (select a1.id , a1.name , t.date ,
time1 = (select top 1 time from a2 where time <= ''12:00'' and a2.id = a1.id and date = t.date order by time),
time2 = (select top 1 time from a2 where time > ''12:00'' and a2.id = a1.id and date = t.date order by time desc)
from a1 , a2 t
where a1.id = t.id and convert(varchar(7),t.date,120) = ''2009-01'') m group by id , name'
exec(@sql)
drop table a1 , a2
/*
id name 2009-01-01_time1 2009-01-01_time2 2009-01-02_time1 2009-01-02_time2
----------- ---------- ---------------- ---------------- ---------------- ----------------
1001 a 08:12 15:33 08:12 15:33
1002 b 08:11 18:22 08:11 18:22
1003 c 08:44 NULL 08:44 NULL
*/
create table a1(id int, name varchar(10))
insert into a1 values(1001 , 'a')
insert into a1 values(1002 , 'b')
insert into a1 values(1003 , 'c')
create table a2(id int, time varchar(10) , date varchar(10))
insert into a2 values(1001 , '08:12','2009-01-01')
insert into a2 values(1001 , '15:33','2009-01-01')
insert into a2 values(1002 , '08:11','2009-01-01')
insert into a2 values(1003 , '08:44','2009-01-01')
insert into a2 values(1002 , '18:22','2009-01-01')
insert into a2 values(1001 , '08:12','2009-01-02')
insert into a2 values(1001 , '15:33','2009-01-02')
insert into a2 values(1002 , '08:11','2009-01-02')
insert into a2 values(1003 , '08:44','2009-01-02')
insert into a2 values(1002 , '18:22','2009-01-02')
go
declare @sql varchar(8000)
set @sql = 'select id , name '
select @sql = @sql + ' , max(case convert(varchar(10),date,120) when ''' + date + ''' then time1 end) [' + date + '_time1]'
+ ' , max(case convert(varchar(10),date,120) when ''' + date + ''' then time2 end) [' + date + '_time2]'
from (select distinct convert(varchar(10),date,120) date from a2) as a
set @sql = @sql + ' from (select a1.id , a1.name , t.date ,
time1 = (select top 1 time from a2 where time <= ''12:00'' and a2.id = a1.id and date = t.date order by time),
time2 = (select top 1 time from a2 where time > ''12:00'' and a2.id = a1.id and date = t.date order by time desc)
from a1 , a2 t
where a1.id = t.id) m group by id , name'
exec(@sql)
drop table a1 , a2
/*
id name 2009-01-01_time1 2009-01-01_time2 2009-01-02_time1 2009-01-02_time2
----------- ---------- ---------------- ---------------- ---------------- ----------------
1001 a 08:12 15:33 08:12 15:33
1002 b 08:11 18:22 08:11 18:22
1003 c 08:44 NULL 08:44 NULL
*/