I am using SQL Server 2000. I am trying to calculate Net Promoter Score or NPS based on the below formula
Formula: (Promoters - Detractors) / Total Questions
- Scores 9 – 10 are considered promoters.
- Scores 0 – 6 are considered detractors.
- Scores 7 – 8 are considered neutral.
I have the following data:
Time Q1 Q2 Q3
----------- ------ ------ ------
2012-03-14 7 7 5
2012-03-15 3 2 5
2012-03-15 7 NA 2
2012-03-15 9 10 NULL
2012-03-15 8 4 4
2012-03-15 NA 6 4
2012-03-16 1 7 4
2012-03-16 NULL 0 5
2012-03-17 9 9 2
2012-03-19 0 0 1
2012-03-19 8 5 4
2012-03-19 1 0 3
The person who originally wrote the database stored NULL values as blanks or NA enter code herein a NVARCHAR format (only god knows why..) so the query I am playing with now uses a ISNUMERIC and im trying not to count blank or NA values.
My query, which doesnt work properly looks like:
SELECT CAST(SUM(CASE WHEN ISNUMERIC([Q1]) != 1 THEN 0
WHEN CAST([Q1] AS int) >= 9 THEN 1
WHEN CAST([Q1] AS int) <= 6 THEN -1
ELSE 0 END)
+ SUM(CASE WHEN ISNUMERIC([Q2]) != 1 THEN 0
WHEN CAST([Q2] AS int) >= 9 THEN 1
WHEN CAST([Q2] AS int) <= 6 THEN -1
ELSE 0 END)
+ SUM(CASE WHEN ISNUMERIC([Q3]) != 1 THEN 0
WHEN CAST([Q3] AS int) >= 9 THEN 1
WHEN CAST([Q3] AS int) <= 6 THEN -1
ELSE 0 END)
AS FLOAT)
/ (SUM(CASE WHEN ISNUMERIC([Q1]) != 1 THEN 0
ELSE 1 END)
+ SUM(CASE WHEN ISNUMERIC([Q2]) != 1 THEN 0
ELSE 1 END)
+ SUM(CASE WHEN ISNUMERIC([Q3]) != 1 THEN 0
ELSE 1 END)
) as [NPS]
FROM [nps]
Can someone point me in the right direction?
Thanks!
As others pointed out, changing the data type of the column from
NVARCHARtoINTwould be ideal.Here is the query that might help you get the result that you need.
Click here to view the demo in SQL Fiddle using SQL Server 2012
Script:
Output: