在我的查询中,我有一个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语句并计算条件输出吗? 有什么建议吗?
答案 0 :(得分:0)
我会在这里使用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