如何使用位列分组?

问题描述 投票:0回答:2

我的表格数据

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,而当组中的最大值为null时将配额设为null。我基本上想为id 3实现NULL。

我看到类似的帖子:Use column of type bit to differentiate group by?

sql-server group-by max bit coalesce
2个回答
3
投票

我认为在转换为max后,您只需要一个简单的int,因为maxbit上不起作用。


-2
投票

似乎您已经考虑了一下,您可以通过以下简单查询来做到这一点:

© www.soinside.com 2019 - 2024. All rights reserved.