Как на PowerQuery реализовать такое:
id | product | category | | product | category | | product | category |
1 | apple | fruit | | apple | fruit | | apple | fruit |
2 | apricot | | | apricot | fruit | | apricot | fruit |
3 | peach | | step 1 => | peach | fruit | step 2 => | peach | fruit |
4 | potato, tomato | vegetable | | potato, tomato | vegetable | | potato | vegetable |
5 | pumpkin | | | pumpkin | vegetable | | tomato | vegetable |
6 | cherry | berry | | cherry | berry | | pumpkin | vegetable |
| | | | | | | cherry | berry |
или, если так будет понятнее, какой либо аналог запросу t-sql
step 1, variant 1 |
---|
Код |
---|
with NotNulls as (select id, category from @SomeTable where category is not null),
OnlyNulls as (select id, category from @SomeTable where category is null),
Combination as (select n.id,
v.category,
row_number() over (partition by n.id order by v.id desc) as rn
from OnlyNulls as n
inner join NotNulls as v
on n.id > v.id),
Result as (select * from combination where rn = 1)
select t.id,
t.category,
coalesce(t.category, t1.category, 0)
from @SomeTable as t
left outer join Result as t1
on t.id = t1.id |
|
step 1, variant 2 |
---|
Код |
---|
select
t.id,
t.category,
coalesce(
t.category,
(select top 1 t1.category from @SomeTable t1 where t1.id < t.id and t1.category is not null order by t1.id desc),
0
)
from @SomeTable t |
|
step 1, variant 3 |
---|
Код |
---|
select t.id,
t.category,
coalesce(t.category, t1.category, 0)
from @SomeTable as t
left outer join (select t2.id,
t5.category
from @SomeTable as t2
cross apply (select max(t3.id) as id
from @SomeTable as t3
where t3.id <= t2.id
and t3.category is not null
) as t4
inner join @SomeTable as t5
on t4.id = t5.id
) as t1
on t.id = t1.id |
|