By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE temp01(
SEQ int,
cat01 numeric(6,0),
cat02 numeric(6,0),
dt_day date,
dt_week date,
dt_month date,
price decimal(10,0)
)
INSERT INTO temp01 VALUES (1, 230, 1, '2019-01-01', '2019-01-05', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (2, 230, 1, '2019-01-02', '2019-01-05', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (3, 230, 1, '2019-01-03', '2019-01-05', '2019-01-31', 13000)
INSERT INTO temp01 VALUES (4, 230, 1, '2019-01-04', '2019-01-05', '2019-01-31', 12000)
INSERT INTO temp01 VALUES (5, 230, 1, '2019-01-05', '2019-01-05', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (6, 230, 1, '2019-01-06', '2019-01-12', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (7, 230, 1, '2019-01-07', '2019-01-12', '2019-01-31', 19000)
INSERT INTO temp01 VALUES (8, 230, 1, '2019-01-08', '2019-01-12', '2019-01-31', 20000)
INSERT INTO temp01 VALUES (9, 230, 1, '2019-01-09', '2019-01-12', '2019-01-31', 21500)
INSERT INTO temp01 VALUES (10, 230, 1, '2019-01-10', '2019-01-12', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (11, 230, 1, '2019-01-11', '2019-01-12', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (12, 230, 1, '2019-01-12', '2019-01-12', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (13, 230, 1, '2019-01-13', '2019-01-19', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (1, 230, 2, '2019-01-01', '2019-01-05', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (2, 230, 2, '2019-01-02', '2019-01-05', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (3, 230, 2, '2019-01-03', '2019-01-12', '2019-01-31', 12000)
INSERT INTO temp01 VALUES (4, 230, 2, '2019-01-04', '2019-01-12', '2019-01-31', 17000)
INSERT INTO temp01 VALUES (5, 230, 2, '2019-01-05', '2019-01-12', '2019-01-31', 22000)
INSERT INTO temp01 VALUES (6, 230, 2,' 2019-01-06', '2019-01-12', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (7, 230, 2,' 2019-01-07', '2019-01-12', '2019-01-31', 23000)
INSERT INTO temp01 VALUES (8, 230, 2, '2019-01-08', '2019-01-12', '2019-01-31', 23200)
INSERT INTO temp01 VALUES (9, 230, 2, '2019-01-09', '2019-01-12', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (10, 230, 2, '2019-01-10', '2019-01-12', '2019-01-31', 24000)
INSERT INTO temp01 VALUES (11, 230, 2, '2019-01-11', '2019-01-12', '2019-01-31', NULL)
INSERT INTO temp01 VALUES (12, 230, 2, '2019-01-12', '2019-01-12', '2019-01-31', NULL)
26 rows affected
with val as
(
select cat01, cat02, min_price = min(price), max_price = max(price),
min_seq = min(case when price is not null then seq end),
max_seq = max(case when price is not null then seq end)
from temp01
group by cat01, cat02
)
select t.*,
new_price = coalesce(t.price,
case when t.seq < v.min_seq then min_price
when t.seq > v.max_seq then max_price
end)
FROM temp01 t
left join val v on t.cat01 = v.cat01
and t.cat02 = v.cat02
SEQ | cat01 | cat02 | dt_day | dt_week | dt_month | price | new_price |
---|---|---|---|---|---|---|---|
1 | 230 | 1 | 2019-01-01 | 2019-01-05 | 2019-01-31 | null | 12000 |
2 | 230 | 1 | 2019-01-02 | 2019-01-05 | 2019-01-31 | null | 12000 |
3 | 230 | 1 | 2019-01-03 | 2019-01-05 | 2019-01-31 | 13000 | 13000 |
4 | 230 | 1 | 2019-01-04 | 2019-01-05 | 2019-01-31 | 12000 | 12000 |
5 | 230 | 1 | 2019-01-05 | 2019-01-05 | 2019-01-31 | null | null |
6 | 230 | 1 | 2019-01-06 | 2019-01-12 | 2019-01-31 | null | null |
7 | 230 | 1 | 2019-01-07 | 2019-01-12 | 2019-01-31 | 19000 | 19000 |
8 | 230 | 1 | 2019-01-08 | 2019-01-12 | 2019-01-31 | 20000 | 20000 |
9 | 230 | 1 | 2019-01-09 | 2019-01-12 | 2019-01-31 | 21500 | 21500 |
10 | 230 | 1 | 2019-01-10 | 2019-01-12 | 2019-01-31 | null | 21500 |
11 | 230 | 1 | 2019-01-11 | 2019-01-12 | 2019-01-31 | null | 21500 |
12 | 230 | 1 | 2019-01-12 | 2019-01-12 | 2019-01-31 | null | 21500 |
13 | 230 | 1 | 2019-01-13 | 2019-01-19 | 2019-01-31 | null | 21500 |
1 | 230 | 2 | 2019-01-01 | 2019-01-05 | 2019-01-31 | null | 12000 |
2 | 230 | 2 | 2019-01-02 | 2019-01-05 | 2019-01-31 | null | 12000 |
3 | 230 | 2 | 2019-01-03 | 2019-01-12 | 2019-01-31 | 12000 | 12000 |
4 | 230 | 2 | 2019-01-04 | 2019-01-12 | 2019-01-31 | 17000 | 17000 |
5 | 230 | 2 | 2019-01-05 | 2019-01-12 | 2019-01-31 | 22000 | 22000 |
6 | 230 | 2 | 2019-01-06 | 2019-01-12 | 2019-01-31 | null | null |
7 | 230 | 2 | 2019-01-07 | 2019-01-12 | 2019-01-31 | 23000 | 23000 |
8 | 230 | 2 | 2019-01-08 | 2019-01-12 | 2019-01-31 | 23200 | 23200 |
9 | 230 | 2 | 2019-01-09 | 2019-01-12 | 2019-01-31 | null | null |
10 | 230 | 2 | 2019-01-10 | 2019-01-12 | 2019-01-31 | 24000 | 24000 |
11 | 230 | 2 | 2019-01-11 | 2019-01-12 | 2019-01-31 | null | 24000 |
12 | 230 | 2 | 2019-01-12 | 2019-01-12 | 2019-01-31 | null | 24000 |
13 | 230 | 2 | 2019-01-13 | 2019-01-19 | 2019-01-31 | null | 24000 |
Warning: Null value is eliminated by an aggregate or other SET operation.