Overview:
When approaching questions with complexity beyond basic aggregation, it can be beneficial to deconstruct the question into components. We can break down the approach into 4 sub-questions:
- How do I determine the total amount paid?
- How do I segment the total amount paid such that I get an amount for each subscription?
- How do I restrict the results to only reflect payments made within the last 6 months?
- How do I sort those results so that the highest paying subscription is ranked first?
Answers to questions:
- Sum the Amount field from the Payments table to calculate the total amount paid, which can be accomplished by dragging the Amount field into the measures box and changing the aggregation function to Sum.
- To segment the results, drag the subscription_id field from the Payments table (a foreign key to the Subscriptions table) into the dimensions box. This will effectively add the subscription_id field to both the SELECT and GROUP BY clauses of the SQL statement.
- Drag the payment_date field into the filters box, select the "Between" operator and choose the Start and End values from the previously created calendar interval.
- Sort the results. This can be done in two ways:
- Click on the Sum of Amount box in the Measures section and selecting "Sort Descending"
- Include a "Sort Rows" step in the data pipeline (depicted below)
Data Location:
Analysis Technique:
SQL Concepts:
- Aggregation Function: COUNT (Distinct)
- WHERE (with Calendar Interval)
- GROUP BY subscription_id
- ORDER BY subscription_id
Chartio Concepts:
Data Transformations:
- None required, though the sort could be applied using the data pipeline
Visualization Approach:
Interactive Mode Configuration and Sample Answer:
*Sample Answer not meant for exact comparison, results may change over time
SQL (with dashboard variables):
SELECT
"pa0"."subscription_id" AS "Subscription Id"
, SUM("pa0"."amount") AS "Total sum of Amount"
FROM "public"."payments" AS "pa0"
WHERE ("Payments"."payment_date"::DATE BETWEEN {CALENDAR_INTERVAL.START} AND {CALENDAR_INTERVAL.END})
GROUP BY "Subscription Id"
ORDER BY "Total sum of Amount" DESC, "Subscription Id" ASC LIMIT 1000;
For SQL outside Chartio
WHERE "Payments"."payment_date" >= CURRENT_DATE - INTERVAL '6 MONTHS'