Categories
Database Development

Select records and related entity count

Given the following select that will count related entities for every record SELECT MAIN.*, SUB.app_count FROM my_table MAIN LEFT JOIN (SELECT MAIN2.p_key, count(DISTINCT T3.app_id) AS app_count FROM my_table MAIN2 JOIN T1 … LEFT JOIN T2 … LEFT JOIN T3 … GROUP BY MAIN2.p_key) SUB ON MAIN.p_key = SUB.p_key WHERE … Trying to simplify it, came […]