I’m trying to write a query that involves a join on JSON array. Following are the tables
PROPOSAL Table:
id details
1 {units: [1, 2]}
2 {units: [1]}
UNITS Table:
id DMA_ID
1 1
2 2
DMA Table:
id name
1 SOLAPUR
2 PUNE
Output:
DMA_NAME, Count of proposals
SOLAPUR 2
PUNE 1
Few of my attempts are,
SELECT
dma.name,
count(
CASE
WHEN EXISTS p.id from proposals_proposal p where (p.details->>'units')::jsonb->>u.unit_id is not null then 1
ELSE 0
)
FROM signs_dma dma JOIN signs_unit u ON dma.id=u.dma_id;
Any help is appreciated. Thanks in advance!
2
Answers
This did the job
You can use next query:
SQL editor online