Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于标识组在SQL Server 2012中是否具有特定值的字段_Sql_Sql Server - Fatal编程技术网

用于标识组在SQL Server 2012中是否具有特定值的字段

用于标识组在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有两列

我的结果表应该是


表示组中某个值的存在。表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 . . . 谢谢您。