

AB
Financial analysts at Relias often need to summarize large operational datasets into a format that makes trends easy to compare across time, region, or product line. Pivot tables are a common way to turn detailed rows into a compact reporting view.
Explain how you would use pivot tables to analyze a large dataset from a Relias financial reporting workflow. In your answer, cover:
Keep the answer practical and analyst-focused. The interviewer is looking for a clear explanation of how pivot-style analysis helps summarize large datasets, how to structure the dimensions and measures, and how SQL aggregations can replicate or extend a pivot table in PostgreSQL.
A pivot table summarizes detailed records by grouping one or more dimensions into rows and columns, then applying an aggregation such as SUM, COUNT, or AVG. For a financial analyst, this makes it easier to compare metrics like revenue, invoice count, or refunds across months, regions, or product lines.
SELECT invoice_month, business_unit, SUM(amount) AS total_revenue
FROM relias_billing_transactions
GROUP BY invoice_month, business_unit
ORDER BY invoice_month, business_unit;
The value of a pivot depends on selecting the right dimensions and measures. In a Relias reporting context, dimensions might include month, customer segment, or product, while measures might include total revenue, average invoice amount, or number of transactions.
SELECT customer_segment, SUM(amount) AS total_revenue, COUNT(*) AS transaction_count
FROM relias_billing_transactions
GROUP BY customer_segment;
PostgreSQL does not have a simple built-in PIVOT keyword, so pivot-style outputs are usually created with conditional aggregation using CASE WHEN. This approach is flexible and works well when the target columns are known in advance, such as months in a quarter or a fixed set of business units.
SELECT business_unit,
SUM(CASE WHEN invoice_month = '2024-01' THEN amount ELSE 0 END) AS jan_revenue,
SUM(CASE WHEN invoice_month = '2024-02' THEN amount ELSE 0 END) AS feb_revenue,
SUM(CASE WHEN invoice_month = '2024-03' THEN amount ELSE 0 END) AS mar_revenue
FROM relias_billing_transactions
GROUP BY business_unit;
Large datasets require attention to filtering, indexing, and data cleanliness before pivoting. If duplicate rows, NULL categories, or inconsistent date logic are not handled first, the pivoted output can be misleading even if the SQL itself is correct.
SELECT TO_CHAR(invoice_date, 'YYYY-MM') AS invoice_month,
COALESCE(business_unit, 'Unknown') AS business_unit,
SUM(amount) AS total_revenue
FROM relias_billing_transactions
WHERE invoice_date >= DATE '2024-01-01'
GROUP BY TO_CHAR(invoice_date, 'YYYY-MM'), COALESCE(business_unit, 'Unknown');