简体   繁体   English

Presto SQL - 用0填充范围内的缺失值

[英]Presto SQL - Fill in missing values within range with 0

Here is the initial table:这是初始表:

id range val
1  -4.   2
1  -2.   1
1. 1.    1
1  3.    2
1  4.    1
2. -1.   1
2. 0.    1
2. 1.    1

Updated Table:更新表:

id range val
1  -4.   2
1. -3.   0
1  -2.   1
1. -1.   0
1. 0.    0
1. 1.    1
1. 2.    0
1  3.    2
1  4.    1
2. -1.   1
2. 0.    1
2. 1.    1
2. 2.    0
2  3.    0
2  4.    0

Desired Logic:期望的逻辑:

  • Each id will have some range.每个id都会有一些范围。
  • For each id , the lower end of the range will be different but the upper end will be the max max value in the entire table.对于每个id ,范围的下限将不同,但上限将是整个表中的最大值。
  • For any missing values within that id's updated range, it will be imputed with 0对于该 id 更新范围内的任何缺失值,它将被归为 0

I am no SQL wiz so i'm a bit lost.我不是 SQL wiz,所以我有点迷路了。 Any help would be much appreciated.任何帮助将非常感激。 Let me know if anything is confusing about the logic.让我知道是否对逻辑感到困惑。

If for each range value, you have at least one value, then I think you want:如果对于每个范围值,您至少有一个值,那么我认为您想要:

select i.id, t.range, coalesce(t.value, 0) as value
from (select distinct id from t) i cross join
     (select distinct range from t where range > 0) r left join
     t
     on t.id = i.id and t.range = r.range
union all
select id, range, value
from t
where range <= 0;

How does this work?这是如何运作的? The first subquery generates all combinations of the ids and range values, where the range value is greater than 0. It does this using a Cartesian product of the distinct id and range values.第一个子查询生成 id 和范围值的所有组合,其中范围值大于 0。它使用不同idrange值的笛卡尔积来执行此操作。

The LEFT JOIN then brings in the existing data.然后LEFT JOIN会引入现有数据。 And the final subquery brings in the detritus -- the values less than 0.最后的子查询带来了碎屑——小于 0 的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM