public inbox for [email protected]
help / color / mirror / Atom feedFrom: Stefan Houtzager <[email protected]>
To: David G. Johnston <[email protected]>
Cc: pgsql-novice <[email protected]>
Subject: Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null
Date: Wed, 6 Jan 2021 16:59:44 +0100
Message-ID: <CANJtTDk-iqkiUvLLMxCf9dgwtMZi7vhmwzSKXejgc4d9wvbMpg@mail.gmail.com> (raw)
In-Reply-To: <CAKFQuwb+d5sfx2ALRcM-H3N=aK4wH32j42BdJJpcOuOszEo4Cw@mail.gmail.com>
References: <CANJtTDkpE980enFKd1ViGq569GYFwPzOEnKguyY8y84Zjok4Tg@mail.gmail.com>
<CAKFQuwb+d5sfx2ALRcM-H3N=aK4wH32j42BdJJpcOuOszEo4Cw@mail.gmail.com>
Thanks David!
When I run the following in pgadmin I get the error that follows at the
bottom. Any idea how to get the query right?
select
json_agg(
(select row_to_json(_) from
(select vc.id, vc.descr, vc.expense, vc.version,
vat_percentage) as _
)
)
from vat_cat vc
left join (
select
vatcat_id,
COALESCE(NULLIF(
json_agg(
(select row_to_json(_) from
(select vp.percentage, vp.version,
json_build_object(
'lower', lower(vp.validity),
'upper', upper(vp.validity),
'lower_inc', lower_inc(vp.validity),
'upper_inc', upper_inc(vp.validity)
) validity)
as _)
),
'null'::json), '[]'::json) vat_percentage
from vat_percentage vp
group by vatcat_id
) vp on vc.id = vp.vatcat_id
ERROR: operator does not exist: json = json LINE 11: COALESCE(NULLIF( ^
HINT: No operator matches the given name and argument types. You might need
to add explicit type casts. SQL state: 42883 Character: 306
On Wed, Jan 6, 2021 at 4:10 PM David G. Johnston <[email protected]>
wrote:
> On Wed, Jan 6, 2021 at 3:11 AM Stefan Houtzager <
> [email protected]> wrote:
>
>> COALESCE(
>> json_agg(
>> (select row_to_json(_) from
>> (select vp.percentage, vp.version,
>> json_build_object(
>> 'lower', lower(vp.validity),
>> 'upper', upper(vp.validity),
>> 'lower_inc', lower_inc(vp.validity),
>> 'upper_inc', upper_inc(vp.validity)
>> ) validity)
>> as _)
>> ) FILTER (WHERE vp.vatcat_id IS NOT NULL), '[]'::JSON)
>> vat_percentage
>> { "vat_cat": { "id": 10, "descr": "nonsense", "expense": true, "version":
>> 1, "vat_percentage": null } }
>>
>> How do I get the query right so that it display [] instead of null?
>>
>
> Use COALESCE(NULLIF(..., 'null'::json), '[]'::json); NULLIF converts the
> JSON null into SQL NULL which the COALESCE then replaces with a empty json
> array.
>
> David J.
>
>
--
Kind regards,
Stefan Houtzager
Houtzager ICT consultancy & development
www.linkedin.com/in/stefanhoutzager
view thread (7+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected]
Subject: Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null
In-Reply-To: <CANJtTDk-iqkiUvLLMxCf9dgwtMZi7vhmwzSKXejgc4d9wvbMpg@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox