I am trying to join 2 tables into one table book (they have one-to-one relation) and then nest a one-to-many relation ebooks
If I try to nest the one-to-many results it works fine:
select r.*, array_agg(e) e_books from gardner_record r
left join gardner_e_book_record e on r.ean_number = e.physical_edition_ean
group by r.id
This works as expected. I get the results back but there is a few more columns I need to add to gardner_record from another table with one-to-one relationship called gardner_inventory_item
This is what I have tried:
select book.*, array_agg(e) e_books from (
    select  r.*,
           i.price,
           i.discount,
           i.free_stock,
           i.report,
           i.report_date
    from gardner_record r
             inner join gardner_inventory_item i on r.ean_number = i.ean
    ) book
left join gardner_e_book_record e on book.ean_number = e.physical_edition_ean
group by book.id
But it doesn't work. The query gives error that modified_date (a column on gardner_record) needs to be in group by or aggregated.
Why is this? I make a single table in subquery. Then I left join the nested records as ebooks and use the array_agg() in select. Shouldn't that mean it is all aggregated?  It works in first query.
What am I missing conceptually here?
 
     
    