---- Crosstabs Using Aggregate FILTER Clause WITH cte As ( SELECT salesman , COALESCE(SUM(order_total) FILTER ( WHERE order_month = 1), 0) AS Jan, COALESCE(SUM(order_total) FILTER ( WHERE order_month = 2), 0) AS Feb, COALESCE(SUM(order_total) FILTER ( WHERE order_month = 3), 0) AS Mar, COALESCE(SUM(order_total) FILTER ( WHERE order_month = 4), 0) AS Apr, COALESCE(SUM(order_total), 0) AS total FROM ( SELECT e.last_name || ' ' || e.first_name AS salesman , date_part('month', order_date) AS order_month, ROUND((quantity * unit_price)::numeric, 2) AS order_total FROM employees e JOIN orders o ON e.employee_id = o.employee_id JOIN order_details od ON o.order_id = od.order_id WHERE date_part('year', order_date) = 1997 AND date_part('month', order_date) BETWEEN 1 AND 4 ) employee_sales GROUP by 1 ) SELECT * FROM cte UNION ALL SELECT 'Totals ', SUM(jan), SUM(feb), SUM(mar), SUM(apr), SUM(total) AS total FROM cte