Database Views
Using database views for complex reporting queries.
Overview
Database views simplify complex queries by predefining joins and aggregations:
CREATE VIEW employee_time_summary AS
SELECT
e.id AS employee_id,
u."firstName",
u."lastName",
o.name AS organization_name,
SUM(tl.duration) AS total_duration,
COUNT(DISTINCT tl.id) AS log_count,
MAX(tl."stoppedAt") AS last_tracked
FROM employee e
JOIN "user" u ON e."userId" = u.id
JOIN organization o ON e."organizationId" = o.id
LEFT JOIN time_log tl ON tl."employeeId" = e.id
GROUP BY e.id, u."firstName", u."lastName", o.name;