Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I include a NULL value using array_agg in postgresql?

If I query this :

SELECT DISTINCT class_low
FROM groups NATURAL JOIN species
WHERE type ~~ 'faune'
AND class_high ~~ 'Arachnides'
AND (class_middle ~~ 'Araignées' OR class_middle IS NULL)
AND (class_low ~~ '%' OR class_low IS NULL);

I get :

      class_low      
---------------------
 Dictynidés
 Linyphiidés

 Sparassidés
 Metidés
 Thomisidés
 Dolomedidés
 Pisauridés
 Araignées sauteuses
 Araneidés
 Lycosidés
 Atypidés
 Pholcidés
 Ségestriidés
 Tetragnathidés
 Miturgidés
 Agelenidés

Notice the NULL value (it's not a empty varchar).

now if I query like that :

SELECT array_to_string(array_agg(DISTINCT class_low), ',')
FROM groups NATURAL JOIN species
WHERE type ~~ 'faune'
AND class_high ~~ 'Arachnides'
AND (class_middle ~~ 'Araignées' OR class_middle IS NULL)
AND (class_low ~~ '%' OR class_low IS NULL);

I get :

      array_to_string                                                                                      
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Agelenidés,Araignées sauteuses,Araneidés,Atypidés,Dictynidés,Dolomedidés,Linyphiidés,Lycosidés,Metidés,Miturgidés,Pholcidés,Pisauridés,Ségestriidés,Sparassidés,Tetragnathidés,Thomisidés

The NULL value is not inserted.

Is there any way to include it ? I mean having something like :

...,,... (just a double colon)

like image 765
vdegenne Avatar asked May 25 '13 18:05

vdegenne


People also ask

How do you represent NULL in PostgreSQL?

The PostgreSQL NULL is the term used to represent a missing value. A NULL value in a table is a value in a field that appears to be blank. A field with a NULL value is a field with no value. It is very important to understand that a NULL value is different from a zero value or a field that contains spaces.

How do I SELECT NULL values in PostgreSQL?

Example - With INSERT Statement INSERT INTO contacts (first_name, last_name) SELECT first_name, last_name FROM employees WHERE employee_number IS NULL; This PostgreSQL IS NULL example will insert records into the contacts table where the employee_number contains a NULL value.

What is Array_agg in Postgres?

PostgreSQL ARRAY_AGG() function is an aggregate function that accepts a set of values and returns an array where each value in the input set is assigned to an element of the array. Syntax: ARRAY_AGG(expression [ORDER BY [sort_expression {ASC | DESC}], [...]) The ORDER BY clause is an voluntary clause.

How do I deal with not null in PostgreSQL?

Here is an example of how to use the PostgreSQL IS NOT NULL condition in a SELECT statement: SELECT * FROM employees WHERE first_name IS NOT NULL; This PostgreSQL IS NOT NULL example will return all records from the employees table where the first_name does not contain a null value.


1 Answers

I don't have an 8.4 handy but in more recent versions, the array_to_string is ignoring your NULLs so the problem isn't array_agg, it is array_to_string.

For example:

=> select distinct state from orders;
  state  
---------

 success
 failure

That blank line is in fact a NULL. Then we can see what array_agg and array_to_string do with this stuff:

=> select array_agg(distinct state) from orders;
       array_agg        
------------------------
 {failure,success,NULL}

=> select array_to_string(array_agg(distinct state), ',') from orders;
 array_to_string 
-----------------
 failure,success

And the NULL disappears in the array_to_string call. The documentation doesn't specify any particular handling of NULLs but ignoring them seems as reasonable as anything else.

In version 9.x you can get around this using, as usual, COALESCE:

=> select array_to_string(array_agg(distinct coalesce(state, '')), ',') from orders;
 array_to_string  
------------------
 ,failure,success

So perhaps this will work for you:

array_to_string(array_agg(DISTINCT coalesce(class_low, '')), ',')

Of course that will fold NULLs and empty strings into one value, that may or may not be an issue.

like image 132
mu is too short Avatar answered Oct 02 '22 22:10

mu is too short