目前我有一个包含3个主要维度的交易列表:Keys,Time_Index和Cumulative_Amount。
Keys Time_Index Cumulative_Amount
100 1 103
100 3 222
101 2 101
我正在尝试“填充所有Time_Index”。在这种情况下,虽然在时间2关键100没有发生任何事情,但我想填充它。结果应如下(最大时间指数= 3):
Keys Time_Index Cumulative_Amount
100 1 103
100 2 103
100 3 222
101 2 101
101 3 101
请注意,我没有为密钥101填充时间1,因为密钥101存在的最早时间是时间2.我将填充每个密钥,直到max_time_index = 3。
答案 0 :(得分:0)
I would first generate a list of all possible combinations of Keys
and Time_Index
, followed by a usual RIGHT / LEFT OUTER JOIN
where you can do a condition for NULL
. Something like this:
with t as
(
select distinct x = a.number, y = b.number
from master..[spt_values] a
cross join
master..[spt_values] b
where a.number between 1 and 4
and b.number between 100 and 103
)
select
case when x.Cumulative_Amount is null then -1 else x.Cumulative_Amount end, t.Keys, t.Time_Index
from dbo.myTable x
right join t on t.Keys = x.Keys