2012-11-21 18 views
1

我有一个查询,其提供在数据库中用于事物不同类别的细分:在SQL分组空和空值作为一个

select categories, count(*) from products 
group by categories 

的有关资料是这样的:

NULL    56 
        42 
FooCategory   12 
BlahCategory  2 

我想团体NULL<blank>

NoCategory   98 
FooCategory   12 
BlahCategory  2 

回答

2

尝试

select categories, 
case when categories is null or categories = ' ' 
then 'noCategory' else categories end as grouped, 
count(*) 
from products 
group by grouped 
+0

这对我来说很好,t我很可能会修整列并将其与空字符串进行比较以匹配“'或全部空格。 – JAQFrost

+0

是的,你可以用任何你想要的条件来扩展它 – Beth