case语句,hadoop

ncecgwcz  于 2021-06-27  发布在  Hive
关注(0)|答案(5)|浏览(315)

我的电子邮件地址谁已经注册了一次零价值,然后再与一定数额的附件。我可以用case语句进行一个不同的电子邮件查询,但我正在努力解决的是,如果一封电子邮件的总金额为零值,那么在表的其余部分搜索值大于0的电子邮件,并将该电子邮件地址作为不同的地址。
有什么建议吗?
示例数据:包含两个字段的表-电子邮件和总额

Email: abc@gmail.com | Gross Amt: $0    
Email: abc@gmail.com | Gross Amt: $50    
Email: xyz@gmail.com | Gross Amt: $0

所需输出:

Email with 0 value: xyz@gmail.com    
Email with >0 value: abc@gmail.com
mzmfm0qo

mzmfm0qo1#

你可以用解析函数来计算。请阅读查询中的注解:

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

除金额为0的记录外,将返回emailgrossamt>0的所有行。每封emailgrossamt=0的电子邮件只返回一条记录
也许它仍然可以优化,但是希望,你有这个想法

xkrw2x1b

xkrw2x1b2#

您可以尝试使用条件聚合

select email,
sum(case when t1.GrossAmt=0 then 1 end) total
sum(case when t1.GrossAmt> then GrossAmt end) 
from table_name 
group by email
7lrncoxx

7lrncoxx3#

您也可以尝试使用row\u number()。

select email, grossamt from (select email, grossamt, row_number() over (partition email order by grossamt desc) as rnk from table) A where a.rnk=1
sg3maiej

sg3maiej4#

这就是挑战。只需要一次值为0的电子邮件,但如果它有两个以上的值&一个值为0,则丢弃0并考虑值>0。
可能正在创建两个表,一个为0,一个为>0,然后加入电子邮件地址工作?从表1中选择email&gross amount1,如果从表1中选择gross amount1=0,那么从表2中选择gross amount=2?

tcomlyy6

tcomlyy65#

尝试以下操作-使用聚合和分组方式

select email, sum(amount)
from tablename
group by email

相关问题