Impala比较连续行,并插入相同的行,如果没有值

发布于 2025-01-25 15:18:30 字数 3420 浏览 3 评论 0原文

我有一个表格,每个月都会为我提供数据,我需要这段时间。我注意到有时我有3/4个月没有数据,但是我需要复制丢失的时间戳的最后一行。

示例:

product_idtotal_revenueeymonth
150202201
217202201
330202201
167202202
231202202
1 67 202202 167202203
231202203
333202203,

但我需要一个输出,例如:

product_IDtote_id tote_id tote_ide_revenueyearth
13502201 2 17 202201
302201 33022013 302201 3 302201
367 67 7 67 7302201 3 302201 3 302201 3 302201
3302201 1 67 6767 7 67 202202
231202202
330202202
167202203
231202203
333202203

我有一个精选的陈述,例如:

select 
    product_id, total_revenue, yearmonth 
from 
    revenue

我找到了类似的问题, (如果没有值)插入相同的行,但是在Impala中,我没有横向连接,有人知道我该怎么做吗?

I have a table that gives me data every month and I need that range of time. I noticed that sometimes I don't have data for 3/4 month but I need to duplicate the last row available with the missing timestamp.

Example:

product_idtotal_revenueyearmonth
150202201
217202201
330202201
167202202
231202202
167202203
231202203
333202203

But I need an output like:

product_idtotal_revenueyearmonth
150202201
217202201
330202201
167202202
231202202
330202202
167202203
231202203
333202203

I have a select statement like:

select 
    product_id, total_revenue, yearmonth 
from 
    revenue

I found a similar question, (Postgresql compare consecutive rows and insert identical row if there are no values) but in Impala I have not the lateral join, does anybody know how can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2025-02-01 15:18:30

我做到了!

with crossed as
(
select
product_id,id_month,
rank() over (partition by product_id order by id_month asc) as r
from
(
select distinct cast(id_month as string) as id_month
from calendar d
where day_data <= date_sub(now(), interval 1 month)
) a
cross join
(select product_id, min(concat(year,month)) as minimum
from revenue
group by product_id
) b
where a.id_month >= b.minimum
)
, created as
(
select
coalesce(a.product_id,b.product_id) as product_id,
coalesce(concat(a.year,a.month),b.id_month) as id_month,
a.total_revenue,
b.r
from revenue a
full outer join crossed b
on a.product_id=b.product_id and concat(a.year,a.month)=b.id_month
where a.year is null
)
,
real as
(
select
coalesce(a.product_id,b.product_id) as product_id,
coalesce(concat(a.year,a.month),b.id_month) as id_month,
a.total_revenue,
b.r
from revenue a
full outer join crossed b
on a.product_id=b.product_id and concat(a.year,a.month)=b.id_month
where a.year is not null
)
select product_id,id_month,total_revenue,'CREATED' as tipe
from
(
select created.product_id,created.id_month,real.total_revenue,
rank () over (partition by created.product_id,created.id_month order by (created.r-real.r) asc) as r
from
created left join real on created.product_id=real.product_id
and created.id_month > real.id_month
)a
where r=1
union
select product_id,concat(year,month) as id_month,total_revenue,'REAL' as tipe
from revenue

I did it!

with crossed as
(
select
product_id,id_month,
rank() over (partition by product_id order by id_month asc) as r
from
(
select distinct cast(id_month as string) as id_month
from calendar d
where day_data <= date_sub(now(), interval 1 month)
) a
cross join
(select product_id, min(concat(year,month)) as minimum
from revenue
group by product_id
) b
where a.id_month >= b.minimum
)
, created as
(
select
coalesce(a.product_id,b.product_id) as product_id,
coalesce(concat(a.year,a.month),b.id_month) as id_month,
a.total_revenue,
b.r
from revenue a
full outer join crossed b
on a.product_id=b.product_id and concat(a.year,a.month)=b.id_month
where a.year is null
)
,
real as
(
select
coalesce(a.product_id,b.product_id) as product_id,
coalesce(concat(a.year,a.month),b.id_month) as id_month,
a.total_revenue,
b.r
from revenue a
full outer join crossed b
on a.product_id=b.product_id and concat(a.year,a.month)=b.id_month
where a.year is not null
)
select product_id,id_month,total_revenue,'CREATED' as tipe
from
(
select created.product_id,created.id_month,real.total_revenue,
rank () over (partition by created.product_id,created.id_month order by (created.r-real.r) asc) as r
from
created left join real on created.product_id=real.product_id
and created.id_month > real.id_month
)a
where r=1
union
select product_id,concat(year,month) as id_month,total_revenue,'REAL' as tipe
from revenue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文