public inbox for [email protected]
help / color / mirror / Atom feedcoalesce(json_agg [..] filter where [..], '[]' in left join returning null
7+ messages / 2 participants
[nested] [flat]
* coalesce(json_agg [..] filter where [..], '[]' in left join returning null
@ 2021-01-06 10:11 Stefan Houtzager <[email protected]>
2021-01-06 15:09 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null David G. Johnston <[email protected]>
0 siblings, 1 reply; 7+ messages in thread
From: Stefan Houtzager @ 2021-01-06 10:11 UTC (permalink / raw)
To: [email protected]
This query
select
json_agg(
json_build_object(
'vat_cat',
(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(
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
from vat_percentage vp
group by vatcat_id
) vp on vc.id = vp.vatcat_id`
Gives as one of it's vat_cat (with no related vat_percentage records)
results
{ "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?
--
Kind regards,
Stefan Houtzager
Houtzager ICT consultancy & development
www.linkedin.com/in/stefanhoutzager
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null
2021-01-06 10:11 coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
@ 2021-01-06 15:09 ` David G. Johnston <[email protected]>
2021-01-06 15:59 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
0 siblings, 1 reply; 7+ messages in thread
From: David G. Johnston @ 2021-01-06 15:09 UTC (permalink / raw)
To: Stefan Houtzager <[email protected]>; +Cc: pgsql-novice <[email protected]>
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.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null
2021-01-06 10:11 coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
2021-01-06 15:09 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null David G. Johnston <[email protected]>
@ 2021-01-06 15:59 ` Stefan Houtzager <[email protected]>
2021-01-06 16:10 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null David G. Johnston <[email protected]>
0 siblings, 1 reply; 7+ messages in thread
From: Stefan Houtzager @ 2021-01-06 15:59 UTC (permalink / raw)
To: David G. Johnston <[email protected]>; +Cc: pgsql-novice <[email protected]>
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
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null
2021-01-06 10:11 coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
2021-01-06 15:09 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null David G. Johnston <[email protected]>
2021-01-06 15:59 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
@ 2021-01-06 16:10 ` David G. Johnston <[email protected]>
2021-01-06 16:32 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
0 siblings, 1 reply; 7+ messages in thread
From: David G. Johnston @ 2021-01-06 16:10 UTC (permalink / raw)
To: Stefan Houtzager <[email protected]>; +Cc: pgsql-novice <[email protected]>
On Wed, Jan 6, 2021 at 8:59 AM Stefan Houtzager <[email protected]>
wrote:
>
> 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
>
>
Right, json doesn't implement equality. You will need to cast to and
compare text values.
David J.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null
2021-01-06 10:11 coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
2021-01-06 15:09 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null David G. Johnston <[email protected]>
2021-01-06 15:59 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
2021-01-06 16:10 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null David G. Johnston <[email protected]>
@ 2021-01-06 16:32 ` Stefan Houtzager <[email protected]>
2021-01-06 16:38 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null David G. Johnston <[email protected]>
0 siblings, 1 reply; 7+ messages in thread
From: Stefan Houtzager @ 2021-01-06 16:32 UTC (permalink / raw)
To: David G. Johnston <[email protected]>; +Cc: pgsql-novice <[email protected]>
Thanks again David. I am very novice, and tried the following which does
not give an error but returns null instead of []
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 _)
)::text,
'null'::text), '[]'::text) vat_percentage
from vat_percentage vp
group by vatcat_id
) vp on vc.id = vp.vatcat_id
On Wed, Jan 6, 2021 at 5:10 PM David G. Johnston <[email protected]>
wrote:
> On Wed, Jan 6, 2021 at 8:59 AM Stefan Houtzager <
> [email protected]> wrote:
>
>>
>> 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
>>
>>
> Right, json doesn't implement equality. You will need to cast to and
> compare text values.
>
> David J.
>
>
--
Kind regards,
Stefan Houtzager
Houtzager ICT consultancy & development
www.linkedin.com/in/stefanhoutzager
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null
2021-01-06 10:11 coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
2021-01-06 15:09 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null David G. Johnston <[email protected]>
2021-01-06 15:59 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
2021-01-06 16:10 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null David G. Johnston <[email protected]>
2021-01-06 16:32 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
@ 2021-01-06 16:38 ` David G. Johnston <[email protected]>
2021-01-07 13:22 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
0 siblings, 1 reply; 7+ messages in thread
From: David G. Johnston @ 2021-01-06 16:38 UTC (permalink / raw)
To: Stefan Houtzager <[email protected]>; +Cc: pgsql-novice <[email protected]>
On Wed, Jan 6, 2021 at 9:32 AM Stefan Houtzager <[email protected]>
wrote:
> Thanks again David. I am very novice, and tried the following which does
> not give an error but returns null instead of []
>
Please don't top-post here.
The general concept should work. You may at least wish to experiment in a
simple query to get the feel for the mechanics. With respect to this query
I'd probably need to have a working query to play with (and time) to give a
more precise answer.
David J.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null
2021-01-06 10:11 coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
2021-01-06 15:09 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null David G. Johnston <[email protected]>
2021-01-06 15:59 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
2021-01-06 16:10 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null David G. Johnston <[email protected]>
2021-01-06 16:32 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
2021-01-06 16:38 ` Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null David G. Johnston <[email protected]>
@ 2021-01-07 13:22 ` Stefan Houtzager <[email protected]>
0 siblings, 0 replies; 7+ messages in thread
From: Stefan Houtzager @ 2021-01-07 13:22 UTC (permalink / raw)
To: David G. Johnston <[email protected]>; +Cc: pgsql-novice <[email protected]>
On Wed, Jan 6, 2021 at 5:38 PM David G. Johnston <[email protected]>
wrote:
>
> Please don't top-post here.
>
> The general concept should work. You may at least wish to experiment in a
> simple query to get the feel for the mechanics. With respect to this query
> I'd probably need to have a working query to play with (and time) to give a
> more precise answer.
>
> David J.
>
Thanks David,
I do not get what you suggest working within the left-join. What does work
is the following:
select json_agg(_)
from (
select
vc.id,
vc.descr,
vc.expense,
vc.version,
(select coalesce(json_agg(_), '[]')
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
from vat_percentage vp
where vp.vatcat_id = vc.id
) _
) as percentages
from vat_cat vc
group by vc.id
) _
--
Kind regards,
Stefan Houtzager
Houtzager ICT consultancy & development
www.linkedin.com/in/stefanhoutzager
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2021-01-07 13:22 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-01-06 10:11 coalesce(json_agg [..] filter where [..], '[]' in left join returning null Stefan Houtzager <[email protected]>
2021-01-06 15:09 ` David G. Johnston <[email protected]>
2021-01-06 15:59 ` Stefan Houtzager <[email protected]>
2021-01-06 16:10 ` David G. Johnston <[email protected]>
2021-01-06 16:32 ` Stefan Houtzager <[email protected]>
2021-01-06 16:38 ` David G. Johnston <[email protected]>
2021-01-07 13:22 ` Stefan Houtzager <[email protected]>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox