Вы можете сделать это, используя аналитические функции.Пожалуйста, прочитайте комментарии в запросе:
select case when emailGrossAmt=0 then 'Email with 0 value'
when emailGrossAmt>0 then 'Email with > 0 value'
end as grp,
email,
GrossAMT
from
(
select s.*,
case when emailGrossAmt=0 then row_number() over(partition by email) else 1 end zero_rn --we need only one record with zeroAMT
from
(
select email, GrossAMT,
sum(GrossAmt) over(partition by email) emailGrossAmt,
dense_rank() over(partition by email order by case when GrossAmt=0 then 1 else 0 end) rnk --to pass all >0, they will have rnk=1
from
( --replace this subquery(s) with your table
select stack(5,
'abc@gmail.com',0 ,
'abc@gmail.com',50 ,
'abc@gmail.com',500 ,
'xyz@gmail.com',0,
'xyz@gmail.com',0 ) as (email, GrossAMT)
) s --your table
) s
where rnk=1
)s where zero_rn=1
Результат:
Email with > 0 value abc@gmail.com 500
Email with > 0 value abc@gmail.com 50
Email with 0 value xyz@gmail.com 0
Возвращаются все строки с emailGrossAmt> 0, кроме записей с количеством 0.Возвращается только одна запись по электронной почте с emailGrossAmt = 0
Возможно, она все еще может быть оптимизирована, но, надеюсь, у вас есть идея