Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null

From: Stefan Houtzager <stefan(dot)houtzager(at)gmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: pgsql-novice <pgsql-novice(at)lists(dot)postgresql(dot)org>
Subject: Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null
Date: 2021-01-07 13:22:02
Message-ID: CANJtTDkecQ0h1rYrSQc_g=2E6mdAdC8citm-CLA8z4_yhmgvjw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Wed, Jan 6, 2021 at 5:38 PM David G. Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>
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

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Santosh Udupi 2021-01-21 01:32:44 Postgres 12 - Generated Columns - Backup/Restore
Previous Message David G. Johnston 2021-01-06 16:38:06 Re: coalesce(json_agg [..] filter where [..], '[]' in left join returning null