如何解决当前行为null时,将最后一个值用于PostgreSQL时间序列表
我遇到一个找不到最佳解决方案的问题。因此,想法是从商店列表中获取商品列表在每个给定时间的价格,但是由于价格是在不同的时间记录的,因此在按时间分组时我会得到一些空值,并且还会得到一个数组o值。因此,它需要采取几个步骤才能获得我所需要的。我想知道是否有人知道更好,更快的方法来实现这一目标。贝娄是我最初的PostgreSQL表,当然,这只是它的摘录:
初始表
下面是提供我想要的结果的PostgreSQL sql代码,但这看起来非常昂贵:
SELECT times,first_value(price_yami_egg) OVER (PARTITION BY partition_price_yami_egg order by time) as price_yami_egg
first_value(price_yami_salt) OVER (PARTITION BY partition_price_yami_salt order by time) as price_yami_salt
first_value(price_dobl_egg) OVER (PARTITION BY partition_price_dobl_egg order by time) as price_dobl_egg
first_value(price_dobl_salt) OVER (PARTITION BY partition_price_dobl_salt order by time) as price_dobl_salt
FROM(
SELECT time,min(price_yami_egg) as price_yami_egg,sum(case when min(price_yami_egg) is not null then 1 end) over (order by times) as partition_price_yami_egg
min(price_yami_salt) as price_yami_salt,sum(case when min(price_yami_salt) is not null then 1 end) over (order by times) as partition_price_yami_salt
min(price_dobl_egg) as price_dobl_egg,sum(case when min(price_dobl_egg) is not null then 1 end) over (order by times) as partition_price_dobl_egg
min(price_dobl_salt) as price_dobl_salt,sum(case when min(price_dobl_salt) is not null then 1 end) over (order by times) as partition_price_dobl_salt
FROM (
SELECT "time" AS times,CASE WHEN shop_name::text = 'yami'::text AND product_name::text = 'egg'::text THEN price END AS price_yami_egg
CASE WHEN shop_name::text = 'yami'::text AND product_name::text = 'salt'::text THEN price END AS price_yami_salt
CASE WHEN shop_name::text = 'dobl'::text AND product_name::text = 'egg'::text THEN price END AS price_dobl_egg
CASE WHEN shop_name::text = 'dobl'::text AND product_name::text = 'salt'::text THEN price END AS price_dobl_salt
FROM shop sh
) S
GROUP BY time
ORDER BY time) SS
解决方法
您只想聚合吗?
select time,min(price) filter (where shop_name = 'Yami' and product_name = 'EGG'),min(price) filter (where shop_name = 'Yami' and product_name = 'SALT'),min(price) filter (where shop_name = 'Dobl' and product_name = 'EGG'),min(price) filter (where shop_name = 'Dobl' and product_name = 'SALT')
from shop s
group by time;
如果。您关心的是结果中的NULL
值,然后可以将其填充。这有点棘手,但想法是:
with t as (
select time,min(price) filter (where shop_name = 'Yami' and product_name = 'EGG') as yami_egg,min(price) filter (where shop_name = 'Yami' and product_name = 'SALT') as yami_salt,min(price) filter (where shop_name = 'Dobl' and product_name = 'EGG') as dobl_egg,min(price) filter (where shop_name = 'Dobl' and product_name = 'SALT') as dobl_salt
from shop s
group by time
)
select s.*,max(yaml_egg) over (yaml_egg_grp) as imputed_yaml_egg,max(yaml_salt) over (yaml_egg_grp) as imputed_yaml_salt,max(dobl_egg) over (yaml_egg_grp) as imputed_dobl_egg,max(dobl_salt) over (yaml_egg_grp) as imputed_dobl_salt
from (select s.*,count(yaml_egg) over (order by time) as yaml_egg_grp,count(yaml_salt) over (order by time) as yaml_egg_grp,count(dobl_egg) over (order by time) as dobl_egg_grp,count(dobl_salt) over (order by time) as dobl_salt_grp
from s
) s
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。