0

I have this table

id | present
------------
A | false
B | null
C | null
D | false
E | false

and perform this query

SELECT array_agg(id)
FROM above_table
WHERE present IS NULL

I expect this to return a single row of

B C

But instead I get an empty row.

If I do

SELECT array_agg(id)
FROM above_table
WHERE present = 'false'

I get a row of

A D E

Any thoughts on why IS NULL does not return the array?

4
  • I believe so. The present column is boolean Commented Apr 18, 2015 at 16:21
  • What do you get for \d above_table in psql? Commented Apr 18, 2015 at 16:26
  • Column | Type | Modifiers ----------------+--------------------------------+------------------------ present | boolean | Commented Apr 18, 2015 at 16:35
  • @aubonphysics any update to your question? Commented Apr 26, 2015 at 16:30

2 Answers 2

1

There is something wrong with your table definition or your test.

If you define column present as boolean and the rows with id B and C really are NULL, then your first query will give what you are expecting.

My console log:

strobel=# create table quest (id character, present boolean);
CREATE TABLE
strobel=# insert into quest values ('A',false);
INSERT 0 1
strobel=# insert into quest values ('D',false);
INSERT 0 1
strobel=# insert into quest values ('E',false);
INSERT 0 1
strobel=# insert into quest(id) values ('B');
INSERT 0 1
strobel=# insert into quest(id) values ('C');
INSERT 0 1
strobel=# select * from quest order by 1;
 id | present 
----+---------
 A  | f
 B  | 
 C  | 
 D  | f
 E  | f
(5 Zeilen)

strobel=# SELECT array_agg(id) FROM quest WHERE present IS NULL;
 array_agg                                                            
-----------
 {B,C}
(1 Zeile)
Sign up to request clarification or add additional context in comments.

2 Comments

This is what I see in the database. But I don't get the results back. I get an empty array.
I can confirm this correct expected behave. Maybe your NULL is not NULLs. Try to set format for NULLs by \pset null '[*** NULL ***]'.
1

Works for me

DDL

CREATE TABLE above_table
    ("id" varchar(1), "present" boolean)
;

DML

INSERT INTO above_table
    ("id", "present")
VALUES
    ('A', false),
    ('B', NULL),
    ('C', NULL),
    ('D', false),
    ('E', false)
;

DQL

SELECT array_agg(id)
FROM   above_table
WHERE  present IS NULL;

Output

array_agg
B,C

I suspect something else is happening. Have you considered that the same table name may be used in multiple schemas and that you are pulling from the correct schema?

3 Comments

I have not considered that. Looking into it.
@aubonphysics The same thing I did. Everything okay. You have to check your table. Or you are in the wrong database. Or ...
@Str. I believe his comment is in response to my last statement regarding the schema, not the table/query stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.