zorm
zorm copied to clipboard
Annotated values
Annotated values are columns returned by the query in addition to columns of the table which is primarily being queried. For example, if we have an organizations
and an employees
table linked by organization_id
, we can annotate the employee count for each organization like so:
WITH employee_counts AS
(SELECT organization_id, count(*) FROM employees GROUP BY organization_id)
SELECT organization_id, name, count AS employee_count FROM organizations JOIN employee_counts USING (organization_id);