Is there a way to fix this ugliness

Поиск
Список
Период
Сортировка
От Tim Uckun
Тема Is there a way to fix this ugliness
Дата
Msg-id CAGuHJrP6aq5baeh_cB7QAP6y1z8ceHkDY2VkF9QP-Y+QhxsdOg@mail.gmail.com
обсуждение исходный текст
Ответы Re: Is there a way to fix this ugliness  (Merlin Moncure <mmoncure@gmail.com>)
Re: Is there a way to fix this ugliness  (Karl Czajkowski <karlcz@isi.edu>)
Список pgsql-general
I am trying to get the child elements of a one to many table to be rolled up into a json field in the parent table. The query I am running is 

select 
    ob.id
     ,case when array_position(array_agg(im.image_type), null) = 1  then '[]' else      json_agg(row_to_json(im.*)) end as images
     from observations ob
   left join images im on ob.id = im.observation_id
group by 1


The reason I have the case statement there is because some observations don't have images but the json_agg(row_to_json function returns [NULL] instead of [] which is what I really want.

Is there a more elegant way to do this?

I searched on the internet and somebody suggested coalesce but no matter what combination I tried I could not make it happen.

Thanks.

В списке pgsql-general по дате отправления:

Предыдущее
От: hubert depesz lubaczewski
Дата:
Сообщение: Very slow queries to stats on 9.3
Следующее
От: Merlin Moncure
Дата:
Сообщение: Re: Is there a way to fix this ugliness