将空值和空值分组为sql中的值

时间:2012-11-21 15:25:55

标签: 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 个答案:

答案 0 :(得分:5)

尝试

select ISNULL(categories,'') as Categories, count(*) from products 
group by ISNULL(categories,'')

<强>更新

在此处查看所需参数

ISNULL ( check_expression , replacement_value )

答案 1 :(得分:3)

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