3

I have a query which provides a breakdown of different categories applied to things in the database:

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

The data comes like this:

NULL 56 42 FooCategory 12 BlahCategory 2 

I would like to group NULL and <blank>

NoCategory 98 FooCategory 12 BlahCategory 2 

2 Answers 2

8

try

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

UPDATE

see here for parameters required

ISNULL ( check_expression , replacement_value )

Sign up to request clarification or add additional context in comments.

Comments

4

try

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

1 Comment

This looks good to me, though I'd probably trim the column and compare to an empty string to match either '' or all whitespace.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.