Я вычисляю совокупный счет (2 значения из двух таблиц). Мне удалось получить следующий результат. Теперь я хочу заменить значения NULL следующим образом: Если его первая строка таблицы, тогда мне нужно вставить первое не пустое значение. Для остальных строк мне нужно вставить последнее ненулевое значение, которое появляется перед текущей строкой. Есть ли способ достичь этого?Заменить Нулевые значения данными из той же таблицы
Date | Count1 | Count2
2016-01-01 00:00:00.000 | NULL | 52
2016-01-02 00:00:00.000 | NULL | 54
2016-01-05 00:00:00.000 | 62 | 55
2016-01-08 00:00:00.000 | NULL | 56
2016-01-11 00:00:00.000 | 91 | NULL
2016-01-12 00:00:00.000 | 92 | 59
Wanted Результат:
Date | Count1 | Count2
2016-01-01 00:00:00.000 | 62 | 52
2016-01-02 00:00:00.000 | 62 | 54
2016-01-05 00:00:00.000 | 62 | 55
2016-01-08 00:00:00.000 | 62 | 56
2016-01-11 00:00:00.000 | 91 | 56
2016-01-12 00:00:00.000 | 92 | 59
Запрос я использовал для создания этого результата:
declare @DateCountOpportunities table(d int primary key, c int, cr int)
insert into @DateCountOpportunities
select
datediff(d, 0, IsNull(CreationDate, StartDate)) as d,
count(*) as OpportunitiesCount,
0
from [JobOpportunities]
group by datediff(d, 0, IsNull(CreationDate, StartDate))
declare @rt int = 0
declare @anchor int
update @DateCountOpportunities set
@rt = cr = @rt + c,
@anchor = d
option (maxdop 1)
declare @DateCountRestaurants table(d int primary key, c int, cr int)
insert into @DateCountRestaurants
select
datediff(d, 0, CreatedAt) as d,
count(*) as RestaurantsCount,
0
from [Restaurants]
group by datediff(d, 0, CreatedAt)
declare @rtRes int = 0
declare @anchorRs int
update @DateCountRestaurants set
@rtRes = cr = @rtRes + c,
@anchorRs = d
option (maxdop 1)
Declare @ResultTable table(DateOpportunities DateTime, DateRestaurant DateTime, RestaurantsCount int, OpportunitiesCount int)
insert into @ResultTable
select isnull(DateOpportunities, DateRestaurants) DateOpportunities, isnull(DateRestaurants, DateOpportunities) DateRestaurant, RestaurantsCount, OpportunitiesCount from
(
select
dateadd(d, d, 0) as DateRestaurants,
cr as RestaurantsCount
from @DateCountRestaurants
) Rs
full outer join
(
select
dateadd(d, d, 0) as DateOpportunities,
cr as OpportunitiesCount
from @DateCountOpportunities
)Opp
on Rs.DateRestaurants = Opp.DateOpportunities
order by DateOpportunities
select DateOpportunities as [Date],
RestaurantsCount
,
OpportunitiesCount from @ResultTable
Это похоже на такие вещи, которая лучше решена путем модификации исходный запрос, который дает результат, вместо того, чтобы брать то, что показано здесь как заданное и накладывать сверху. Каковы ваши базовые данные и каков ваш текущий запрос? –
Если вы делаете кумулятивный счет, я считаю, что ваш ответ лежит в запросе, используемом для генерации выводимого вами вывода. То, что вы хотите сделать, возможно, но может усложниться и, скорее всего, не понадобится. Можете ли вы опубликовать этот запрос? –
@JeroenMostert это сложный запрос, проблема в том, что я получаю кумулятивный счет из двух отдельных таблиц. Я отправлю запрос, если вы хотите – Kira