用于标识组在SQL Server 2012中是否具有特定值的字段
我有一个表,表1有两列 我的结果表应该是用于标识组在SQL Server 2012中是否具有特定值的字段,sql,sql-server,Sql,Sql Server,我有一个表,表1有两列 我的结果表应该是 表示组中某个值的存在。表1非常大,我不想使用任何光标或循环。请给我一个更好的方法,在SQL中使用条件逻辑。以下是一种方法: select column1, (case when sum(case when column2 = 'C' then 1 else 0 end) > 0 then 1 end) as has_c, (case when sum(case when co
表示组中某个值的存在。表1非常大,我不想使用任何光标或循环。请给我一个更好的方法,在SQL中使用条件逻辑。以下是一种方法:
select column1,
(case when sum(case when column2 = 'C' then 1 else 0 end) > 0
then 1
end) as has_c,
(case when sum(case when column2 = 'C' then 1 else 0 end) = 0
then 1
end) as does_not_have_c,
from table1 t1
group by column1;
或者更简单地说:
select column1,
max(case when column2 = 'C' then 1 end) as has_c,
min(case when column2 = 'C' then 0 else 1 end) as does_not_have_c,
from table1 t1
group by column1
“A”和“B”来自哪里?对不起,我现在编辑了图像。它应该是一个表示存在CSuper的位。很好的解决方案。感谢应该是
min(当第2列为'C'时,则为0,否则为1结束),因为第2列中没有\u C
part@AjayGupta . . . 谢谢您。