|
发表于 2020-2-3 18:00:01
|
显示全部楼层
create table ta(id int, value varchar(5), date datetime)
insert ta
select 1, 'aa' , '2017-01-02'
union all select 2, 'bb' , '2017-01-01'
union all select 3, 'cc' , '2017-01-04'
生成一个时间段列:
declare @ta table(t_date datetime)
declare @i datetime
set @i='2017-01-01 00:00:00'
while @i!>'2017-1-31 00:00:00'
begin
insert @ta select convert(varchar(10),@i,120)
select @i=@i+1
end
select 时间=convert(varchar(10),t_date,120),value=isnull(value,0) from @ta a , ta
where convert(varchar(10),t_date,120)*=convert(varchar(10),date,120)
and convert(varchar(10),t_date,120) between '2017-01-02'and '2017-01-05'
时间 value
---------- -----
2017-01-02 aa
2017-01-03 0
2017-01-04 cc
2017-01-05 0
(所影响的行数为 4 行)
|
|