计算case语句的重复值

but5z9lq  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(367)

在我的查询中,我有一个case语句,如果条件匹配,我想将列转换为字符串值。另外,我想知道case语句结果的重复值的总数。例如:

select NULL as column_1,
   CASE WHEN name IS NOT NULL THEN CAST(name as string)
   ELSE '0' END as column_2,
    (select COUNT (cast(column_2 as string)) from input_table
     group by (cast(column_2 as string)) HAVING count(cast(column_2 as string)) > 1) as column_3
    from input_table

我可以同时计算case语句和条件输出吗?有什么建议吗?

kkbh8khc

kkbh8khc1#

我会用cte。不是100%清楚你在找什么,但试试这个。

;WITH Input_table as (

SELECT NULL AS column_1,
       CASE
           WHEN name IS NOT NULL
           THEN CAST(name AS STRING)
           ELSE '0'
       END AS column_2
)

select distinct Column_1, Column_2, count(Column_2) [Cnt] from Input_table
group by Column_1, Column_2 
having count(Column_2) > 1

相关问题