How to avoid having long GROUP BY list when joining other tables for data?
Let's suppose you have a simple aggregate query using employee database, which shows number of employees for each project:
SELECT ep.PROJ_ID, count(*)
FROM EMPLOYEE_PROJECT ep
GROUP BY ep.PROJ_ID;
If you need more info about the project itself, you would write something like this:
SELECT ep.PROJ_ID, p.PROJ_NAME, p.PRODUCT, count(*)
FROM EMPLOYEE_PROJECT ep
JOIN PROJECT p on ep.PROJ_ID = p.PROJ_ID
GROUP BY ep.PROJ_ID, p.PROJ_NAME, p.PRODUCT;
When you have a lot of columns involved, list in GROUP BY can get very long. At some point, it would reach a limit for sort key size (see FAQ #236) of 64kB. The prevent this from happening, you can use MIN or MAX for such columns (as they return one value anyway):
SELECT ep.PROJ_ID, MAX(p.PROJ_NAME), MAX(p.PRODUCT), count(*)
FROM EMPLOYEE_PROJECT ep
JOIN PROJECT p on ep.PROJ_ID = p.PROJ_ID
GROUP BY ep.PROJ_ID
This only works when JOIN is on primary key or unique column, usually one referenced by the foreign key. In above example, PROJ_ID is primary key column for table PROJECT. We have primary key for project, and we're just extracting more data from the corresponding row in PROJECT table.
If you use Firebird 2 or above, you can also use derived tables:
SELECT ec.PROJ_ID, p.PROJ_NAME, p.PRODUCT, ec.CNT as EMPLOYEES
FROM (SELECT PROJ_ID, COUNT(*) cnt FROM EMPLOYEE_PROJECT GROUP BY PROJ_ID) ec
JOIN PROJECT p on ec.PROJ_ID = p.PROJ_ID
With Firebird 2.1 and above you can also use CTE (Common Table Expressions):
WITH EMP_COUNT(PROJ_ID, EMPLOYEES)
AS (SELECT PROJ_ID, COUNT(*) FROM EMPLOYEE_PROJECT GROUP BY PROJ_ID)
SELECT ec.PROJ_ID, p.PROJ_NAME, p.PRODUCT, ec.EMPLOYEES
FROM EMP_COUNT ec
JOIN PROJECT p on ec.PROJ_ID = p.PROJ_ID