I need to create table that will hold data for all dates. Balance table for every date. Idea is that if I don't have for particular date data, to get data from the previous day...
I have table like this
2018-09-01 111 1000.00
2018-09-01 222 2000.00
2018-09-02 NULL NULL
2018-09-03 111 2000.00
2018-09-03 222 2000.00
2018-09-04 NULL NULL
2018-09-05 111 NULL
2018-09-05 222 NULL
2018-09-06 NULL NULL
2018-09-07 111 3000.00
2018-09-07 222 10000.00
2018-09-08 NULL NULL
2018-09-09 NULL NULL
2018-09-10 NULL NULL
Output for this should be:
2018-09-01 111 1000.00
2018-09-01 222 2000.00
2018-09-02 111 1000.00
2018-09-02 222 2000.00
2018-09-03 111 2000.00
2018-09-03 222 2000.00
2018-09-04 111 2000.00
2018-09-04 222 2000.00
2018-09-05 111 2000.00
2018-09-05 222 2000.00
2018-09-06 111 2000.00
2018-09-06 222 2000.00
2018-09-07 111 3000.00
2018-09-07 222 10000.00
2018-09-08 111 3000.00
2018-09-08 222 10000.00
2018-09-09 111 3000.00
2018-09-09 222 10000.00
2018-09-10 111 3000.00
2018-09-10 222 10000.00
Can someone help me with this ? I cannot use Max(id)over( or something like this (or I'm wrong :)), because there can be multiple rows for one day.
Thanks in advance...
create table #t
(
date datetime,
userid int,
balance decimal(18,2)
)
insert into #t
select '2018-09-01', 111, 1000
union
select '2018-09-01', 222, 2000
union
select '2018-09-03', 111, 2000
union
select '2018-09-03', 222, 2000
union
select '2018-09-05', 111, NULL
union
select '2018-09-05', 222, NULL
union
select '2018-09-07', 111, 3000
union
select '2018-09-07', 222, 10000
create table #dates
(
date datetime
)
insert into #dates
select '2018-09-01'
union
select '2018-09-02'
union
select '2018-09-03'
union
select '2018-09-04'
union
select '2018-09-05'
union
select '2018-09-06'
union
select '2018-09-07'
union
select '2018-09-08'
union
select '2018-09-09'
union
select '2018-09-10'