更新每个日期的表,并从另一个表添加剩余的站点

时间:2015-01-08 12:29:11

标签: sql sql-server

我有一张桌子'测试'像这样 -

ID  Site    Start Time  End Time    
1   A   30-12-2014 16:06:54 30-12-2014 16:39:52    
2   B   30-12-2014 12:12:50 30-12-2014 12:13:52    
3   C   31-12-2014 12:14:23 31-12-2014 12:15:22    
4   A   01-01-2015 12:20:29 01-01-2015 12:23:32    
5   B   01-01-2015 12:28:49 01-01-2015 12:29:47    

我有另一张表'列表'有一个网站列表 -

Site    
A    
B    
C    

我需要一个输出表,对于每个日期,所有来自' list'的网站包括这样 -

ID  Site    Start Time  End Time    
1   A   30-12-2014 16:06:54 30-12-2014 16:39:52    
2   B   30-12-2014 12:12:50 30-12-2014 12:13:52    
NULL    C   30-12-2014 00:00:00 30-12-2014 00:00:00    
NULL    A   31-12-2014 00:00:00 31-12-2014 00:00:00     
NULL    B   31-12-2014 00:00:00 31-12-2014 00:00:00    
3   C   31-12-2014 12:14:23 31-12-2014 12:15:22    
4   A   01-01-2015 12:20:29 01-01-2015 12:23:32    
5   B   01-01-2015 12:28:49 01-01-2015 12:29:47    
NULL    C   01-01-2015 00:00:00 01-01-2015 00:00:00    

到目前为止,我一直在将表格分开来测试'将每个日期的表格放入中间表格中,然后从“' list"”中选择不匹配的网站。表。我被困在循环中。请帮忙。

这是我的代码 -

ALTER TABLE [test] ADD [DATE] date;

update [test]
set [DATE] = CAST(Start Time] as Date)

select t1.[Site]
from list t1
left join test t2 on t1.[site]=t2.[site] where t2.site is null;

select distinct [DATE] into #Temp1 from [test]
order by [DATE];

select [DATE], row_number()over(order by ([Date])asc) as [Row] into #Temp2 from #Temp1;
drop table #Temp1;

GO
declare @row int
select @row = 0
while ( @row <= (select COUNT(*) from #Temp2))
begin
select @row = 1 + @row
select c.* into #temp3
from( 
select a.* , b.[DATE] as b_date, b.[row]
from test a
inner join #Temp2 b
on a.[Date] = b.[Date] where b.[row] = @row
) c
End;

2 个答案:

答案 0 :(得分:2)

您可以使用select

获得所需的输出
select t.id, l.site, coalesce(t.starttime, d.d) as starttime, coalesce(t.endtime, d.d) as endtime
from list l cross join
     (select distinct cast(starttime as date) as d from test) d left join
     test t
     on t.site = l.site and cast(t.starttime as date) = d.d;

您可以使用类似的逻辑将不匹配的行插入到表中:

insert into test(id, site, starttime, endtime)
    select t.id, l.site, d.d, d.d
    from list l cross join
         (select distinct cast(starttime as date) as d from test) d left join
         test t
         on t.site = l.site and cast(t.starttime as date) = d.d
    where t.site is null;

答案 1 :(得分:0)

试试这个,

Declare @t table(ID int, Site varchar(50),StartTime datetime,EndTime datetime)
insert into @t    values
(1,   'A',   '12-30-2014 16:06:54','12-30-2014 16:39:52'),
(2 ,  'B',   '12-30-2014 12:12:50','12-30-2014 12:13:52'),    
(3  , 'C',   '12-31-2014 12:14:23','12-31-2014 12:15:22'),  
(4 ,  'A',   '01-01-2015 12:20:29','01-01-2015 12:23:32'),
(5,   'B',   '01-01-2015 12:28:49','01-01-2015 12:29:47')   
dECLARE @lIST TABLE(Site varchar(50))
insert into @lIST values('A'),('B'),('C')


;WITH CTE AS
(
SELECT min(cast(StartTime as date)) st FROM @t 
union all
SELECT dateadd(day,1, st) FROM CTE where 
st<casT('01-01-2015 12:28:49' as date)--max date(can be dynamic)
)
,CTE1 as
(
  select * from @lIST a 
  cross apply (select * from cte)b
)
,CTE2 as
(
  select y.ID,x.Site
  ,ISNULL(y.StartTime,x.st)StartTime,ISNULL(y.EndTime,x.st)EndTime
   from CTE1 x
  left join @t y on x.site=y.site and 
  cast(x.st as date)=cast(y.StartTime as date)

)
SELECT * FROM CTE2