**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 Total 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 and Including" operator and choose the Start and End values from the previously created Calendar filter.

Sort the results. This can be done in two ways:

Click on the Total Sum of Amount box in the Measures section and select "Descending" from the Sort dropdown.

Include a "Sort Rows" step in the Data Pipeline (depicted below).

**Data Location**:

**Analysis Technique: **

**SQL Concepts**:

Aggregation Function: SUM(amount)

WHERE (with Calendar filter)

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:**

**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.START} AND {CALENDAR.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'