|
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'
Generate a time period column:
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 time = 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'
Time value
---------- -----
2017-01-02 aa
2017-01-03 0
2017-01-04 cc
2017-01-05 0
(4 rows affected) |
|