My table
id sum type
1 3 -1
1 6 -1
1 -6 2
1 -3 1
1 3 -1
1 6 -1
These
1 3 -1
是空行。类型始终为-1。总和可以不同。
These
1 -6 2
是封闭的行。输入!= -1。 sum = -空行的总和
这些行
1 3 -1
1 6 -1
1 -6 2
1 -3 1
会互相否定。这些
1 3 -1
1 6 -1
会保持。如果最后打开的行数超过 15,那么对于每个打开的行,我需要在表中插入一行,其中 sum 为打开行的 -sum,类型为 2。
Grouping
select id
, sum
, sum(
case
when type = -1 then 1
when type != -1 then -1
else 0
end
) as num
from table
group by id, sum
id sum num
1 3 2
1 6 2
1 -6 2
1 -3 1
可能会帮助我找到组合。但我不知道之后该做什么。
select t.val_id
,t.val_sum
,2 as val_type
from (select val_id
,-abs (val_sum) as val_sum
,sum (case when val_type = -1 then 1 else -1 end) as occurrences
from mytable
group by val_id
,abs (val_sum)
having sum (case when val_type = -1 then 1 else -1 end) > 15
) t
lateral view explode (split (space (cast (occurrences as int) - 1),' ')) e
;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)