Jag
Jag

Reputation: 89

MySQL group by empty and non-empty values

At present, I have something like:

select sum(total) from table_name where field != ''
UNION
select sum(total) from table_name where field = ''

It works but I'm curious if it is possible to use "group by" to filter by empty and non-empty values?

Upvotes: 0

Views: 289

Answers (1)

Sagar Gangwal
Sagar Gangwal

Reputation: 7937

select SUM(CASE WHEN field != '' THEN total ELSE 0) NONEMPTY,
       SUM(CASE WHEN field = '' THEN total ELSE 0) EMPTY from table_name

Try above query.

Here i had used CASE WHEN.

Upvotes: 1

Related Questions