Steve H
Steve H

Reputation: 15

mysql query filter out NULL or blank rows

I am a novice at writing sql, so thank you in advance for any assistance you can provide. I have a table (table1) in a mysql database that views like this:

+-----+----------+------------+
| id  | key      | value      |
+-----+----------+------------+
|   1 | name     | Bob        |
|   1 | location | ABC        |
|   1 | date     | xxxx-xx-xx |
|   2 | name     | Jim        |
|   2 | location | MID        |
|   2 | date     |            |
|   3 | name     |            |
|   3 | location |            |
|   3 | date     |            |
|   4 | name     | Sue        |
|   4 | location | DFW        |
|   4 | date     | xxxx-xx-xx |
|   5 | name     | Sue        |
|   5 | location |            |
|   5 | date     | xxxx-xx-xx |
|   6 | name     | Bob        |
|   6 | location | GRE        |
|   6 | date     | xxxx-xx-xx |
|   7 | name     |            |
|   7 | location |            |
|   7 | date     |            |
+-----+----------+------------+

I created a view where I basically invert (pivot) the rows to columns like this:

+-----+-------+----------+------------+
| id  | name  | location | date       |
+-----+-------+----------+------------+
|   1 | Bob   | ABC      | xxxx-xx-xx |
|   2 | Jim   | MID      |            |
|   3 |       |          |            |
|   4 | Sue   | DFW      | xxxx-xx-xx |
|   5 | Sue   |          | xxxx-xx-xx |
|   6 | Bob   | GRE      | xxxx-xx-xx |
|   7 |       |          |            |
|   8 | Bob   | DFW      | xxxx-xx-xx |
|   9 |       |          |            |
|  10 | Joe   | DFW      | xxxx-xx-xx |
|  11 |       |          |            |
|  12 |       |          |            |
|  13 |       |          |            |
|  14 | Jim   | SUS      | xxxx-xx-xx |
+-----+-------+----------+------------+

Here is the code for how I did this:

select c.`id`, max(ifnull(c.name,NULL)) as name, max(ifnull(c.location,NULL)) as location, max(ifnull(c.date,NULL)) as date from (
select `id`,
case when `key` = 'name' then value end as name,
case when `key` = 'location' then value end as location,
case when `key` = 'date' then value end as date
  from `table1`
  WHERE value != ''
) c group by `id`

I need to filter out the rows from the view where the name, location, and date are NULL or blank. I was able to clear about half of the rows by adding the WHERE value != '', but how do I filter out the rest? WHERE value IS NOT NULL does not seem to help.

Upvotes: 0

Views: 2433

Answers (2)

forpas
forpas

Reputation: 164064

I don't understand why you use IFNULL() the way that you do.
For example max(ifnull(c.name,NULL)) is equivalent to max(c.name).
But you can use it in a HAVING clause to filter out the empty rows:

select c.`id`, 
  max(c.name) as name, 
  max(c.location) as location, 
  max(c.date) as date 
from (
select `id`,
  case when `key` = 'name' then value end as name,
  case when `key` = 'location' then value end as location,
  case when `key` = 'date' then value end as date
  from `table1`
  WHERE value != ''
) c group by `id`
having ifnull(name, '') <> '' or ifnull(location, '') <> '' or ifnull(date, '') <> ''

If there is a case that name, location or date are not just empty strings but contain spaces then use also trim() like:

having ifnull(trim(name), '') <> '' or ifnull(trim(location), '') <> '' or ifnull(trim(date), '') <> ''

Upvotes: 0

Gertjan
Gertjan

Reputation: 56

Could it be that you have spaces as value? Try WHERE TRIM(value) != '' instead.

Try this? WHERE name != '' OR location != '' OR date != ''

Upvotes: 2

Related Questions