I have been trying several solutions but I'm unable to perform a complex query on multiple elements of an aggregated row (elements). This is a similar query that I have done:
select ITEM.id,
ITEM.name,
json_arrayagg(json_array(ELEMENT.name, ELEMENT.code, item_element.count null on null)) elements
from (item_element join item ITEM on (item_element.project_id = ITEM.project_id and
item_element.item_id = ITEM.id) join element ELEMENT
on (item_element.project_id = ELEMENT.project_id and item_element.element_id = ELEMENT.id))
where item_element.project_id = 'project-dev'
group by ITEM.project_id, ITEM.id, ELEMENT.code, item_element.count
having (ELEMENT.code = 10 and item_element.count >= 2) and (ELEMENT.code = 20 and item_element.count <= 4)
What I'm trying to do is aggregate into elements all the item_element that match that specific id with their count. After that I want to be able to concat multiple AND expressions but on the pairs <CODE,COUNT> or <NAME,COUNT>
Is there something I'm missing? Currently when I add more than one COUNT query they are all applied to the row, getting more results than expected.