Common SQL queries and examples for analyzing your Metronome data warehouse exports
This page provides practical SQL query examples for working with your Metronome data exports. Use these queries as starting points for your own analysis and reporting.
All examples use standard SQL syntax. Adjust date functions and syntax as needed for your specific data warehouse (Snowflake, BigQuery, Redshift, etc.).
SELECT DATE_TRUNC("MONTH", i.end_timestamp) AS month, li.line_item_type, SUM(li.total) AS totalFROM invoice iJOIN line_item li ON i.id = li.invoice_idGROUP BY 1, 2ORDER BY 2, 1;
Fetch draft invoice total by contract from most recent snapshot
Get the latest draft invoice totals grouped by contract.
Copy
Ask AI
SELECT contract_id, COUNT(0) AS invoice_count, SUM(total) AS invoice_totalFROM draft_invoiceWHERE snapshot_time = (SELECT MAX(snapshot_time) FROM draft_invoice)GROUP BY 1ORDER BY 1;
Fetch daily draft aggregate line item quantity and total
Analyze line item metrics from the most recent draft breakdowns snapshot.
Copy
Ask AI
WITH max_draft_breakdown_snapshot AS ( SELECT max(snapshot_timestamp) AS max_snapshot_ts FROM breakdowns_draft_invoices)SELECT i.breakdown_start_timestamp, li.name, SUM(li.quantity) AS quantity, SUM(li.total/100) AS total_dollarsFROM breakdowns_draft_invoices iJOIN breakdowns_draft_line_items li ON i.id = li.invoice_breakdown_id AND i.snapshot_timestamp = li.snapshot_timstampWHERE i.environment_type = 'PRODUCTION' AND i.snapshot_timestamp = (SELECT max_snapshot_ts FROM max_draft_breakdown_snapshot) AND li.total >= 0GROUP BY 1, 2ORDER BY 1 DESC;
Fetch daily draft invoice line item totals by customer
Get detailed draft line item breakdowns per customer and invoice.
Copy
Ask AI
WITH max_draft_breakdown_snapshot AS ( SELECT max(snapshot_timestamp) AS max_snapshot_ts FROM breakdowns_draft_invoices)SELECT i.customer_id, i.breakdown_start_timestamp, i.invoice_id, li.name, li.quantity, li.total/100 AS total_dollarsFROM breakdowns_draft_invoices iJOIN breakdowns_draft_line_items li ON i.id = li.invoice_breakdown_id AND i.snapshot_timestamp = li.snapshot_timstampWHERE i.environment_type = 'PRODUCTION' AND i.snapshot_timestamp = (SELECT max_snapshot_ts FROM max_draft_breakdown_snapshot)GROUP BY 1, 2ORDER BY 1 DESC;
Replace <contract_id> with your actual contract ID.
Fetch active rate card entry count per rate card
Analyze rate card coverage by counting active entries.
Copy
Ask AI
SELECT crc.id AS rate_card_id, COUNT(*)FROM contracts_rate_cards crcJOIN contracts_rate_card_entries crce ON crc.id = crce.rate_card_idWHERE crce.ending_before > NOW()GROUP BY 1ORDER BY 1;
SELECT DATE_TRUNC("DAY", ah.created_at) AS alert_date, alert.name, COUNT(alert.id) AS triggered_countFROM customer_alert_history ahJOIN alert ON ah.alert_id = alert.idGROUP BY 1, 2ORDER BY 1;