with
p as (
select p.*
from products as p
order by p.created_at desc
)
select
c.*
coalesce(p.products, '[]'::jsonb) as products
from categories as c
left join lateral (
select jsonb_agg(p.*) as products
from p
where p.category_id = c.id
) as p on true
order by p.products->>'created_at' desc
How do I order by the created_at
column of products
in this query? I want to show categories with the most recently created products first.