This SQL script calculates Net Promoter Score (NPS)-related metrics for...
September 4, 2025 at 05:51 PM
This SQL script calculates Net Promoter Score (NPS)-related metrics for client accounts and updates a reporting table with the results. Here's a breakdown of what it does:
First Section: MAS Business Units
-
Preparation (
WITH dates
):- It identifies three key dates:
startAvgDate
: 24 months ago based on the current date (getdate()
), truncated to the start of the month.endAvgDate
: 1 month before the latest available month in thedocument_date
column.latestDataDate
: The most recent month in thedocument_date
column.
- It identifies three key dates:
-
Calculating Client NPS Metrics (
WITH client_nps_summary
):- For each account within MAS business units:
- Aggregates
likely_to_recommend
values to generate a distinct list of them for reference (likely_To_Recommend_Distinct_Raw
). - Calculates two averages:
rnpsAccountTrendValue_Avg_Raw
: Average NPS value over the 24-month trend period fromstartAvgDate
toendAvgDate
.rnpsAccountAvg_PriorMonth_raw
: Average NPS value for the last month (endAvgDate
onward).
- Aggregates
- For each account within MAS business units:
-
Post-Processing Results (
WITH rnps_results
):- Maps calculated averages to corresponding NPS categories:
- 0.0 to 6.9 →
-100
(Detractors) - 7.0 to 8.9 →
0
(Passives) - 9.0 to 10.0 →
100
(Promoters)
- 0.0 to 6.9 →
- Compares NPS trend values against the prior month's average to classify them into:
- Increasing, Decreasing, or No change trends.
- Maps calculated averages to corresponding NPS categories:
-
Updating Reporting Table:
- Updates columns in
reporting_stg.client_insights_attributes
with the computed metrics:account_average_rnps_current_month
: Prior month's average NPS value.account_average_rnps_trend_24_months
: 24-month trend NPS value.account_description_rnps_trend_indicator
: "Increasing", "Decreasing", or "No change."
- Updates columns in
Second Section: Non-MAS Business Units
The script repeats the same calculation process for Non-MAS business units but with a slight change in the join condition:
- Instead of using
c.oracle_customer__c
, it joins onc.nan_id__c = m.client_iid
.
Key Observations:
- Purpose: The script is used to calculate and classify NPS performance metrics at the account level over two periods (a 24-month trend and the prior month) for both MAS and Non-MAS business units. It then updates the reporting table with these metrics.
- Output: Key insights about how client accounts' NPS scores have changed over time, providing trends and summaries for decision-making.
- Data Scope:
- Applies only to accounts with non-null
likely_to_recommend
values. - Distinguishes between MAS and Non-MAS accounts based on a condition in the
client_insights_attributes
table.
- Applies only to accounts with non-null
Uses:
This type of script is typically used in customer success management, to track customer satisfaction trends, or in business reporting for forecasting and insights on customer loyalty (e.g., NPS).
Generate your own explanations
Download our vscode extension
Read other generated explanations
Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node