如何使用位列进行分组?

贾耶什·克希尔萨加尔

我的表数据

id  quota  
1  0  
1  NULL  
1  1  
2  0  
2  NULL  
3  NULL  

结果我除外

id  quota  
1  1  
2  0  
3  NULL  

结果我得到:

id  quota  
1  1  
2  0  
3  0  

我正在使用的查询:

 select id,count(id) count_id,
 case when max(coalesce(quota,0)) = 1 then 1
 when max(coalesce(quota,0)) = 0 then 0
 else null 
 end as quota from forbit group by i  

我的列配额数据类型是位。

我希望每当组中的最大值为 1 时,将配额设为 1,每当组中的最大值为 0 时,将配额设为 0,每当组中的最大值为空时,将配额设为空。我基本上想为 id 3 实现 NULL。

我看到的类似帖子:使用位类型的列来区分分组依据?

戴尔K

我认为max在转换为 a 之后,您正在寻找一个简单的东西,int因为maxbit.

declare @Test table (id int, quota bit);

insert into @Test (id, quota)
values
(1, 0)
, (1, NULL)
, (1, 1)  
, (2, 0) 
, (2, NULL)  
, (3, NULL);

select id, max(convert(int,quota))
from @Test
group by id;  

返回:

id  quota
1   1
2   0
3   NULL

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章