Skip to content
Advertisement

LISTAGG in SQL is returning a row with null values

I have 2 tables A and B, B is having a foreign key relationship with A i.e.,(b.detail_id = a.id) I want to apply LISTAGG query on one of the column in B.

SELECT LISTAGG(DISTINCT b.delivery_cadence, ',') WITHIN GROUP (ORDER BY b.delivery_cadence) 
delivery_cadence, a.id FROM A a, B b WHERE b.detail_id = a.id AND a.id = 1236565;

The above query is returning me a row with all values as null, But I want no rows. How can I achieve this? If it’s not possible any alternative solution for this.

a.id = 1236565 is not exists in A table.

Advertisement

Answer

just add having count(b.delivery_cadence) > 0 eg

SELECT LISTAGG(DISTINCT b.delivery_cadence, ',') WITHIN GROUP (ORDER BY b.delivery_cadence) 
delivery_cadence, a.id FROM A a, B b WHERE b.detail_id = a.id AND a.id = 1236565
HAVING COUNT(b.delivery_cadence) > 0
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement