I need some help on calculating survey scores by account & month, with a YTD figure for each account each month as well. Similar to NPS, the score is calculated as: Green ratings as % of total responses for account in period - Red ratings as % of total responses for account in period . Amber ratings are included the total response number part of the calculation.
The other requirement is that if an account appears anywhere in the data, then that account must be shown and scored in all months even if there are no responses for them in one or more months - they would be shown as a score of 0 for that month, but they might have a YTD score if there was data for them in previous months.
The data is received monthly from the start of the financial year, and the file always includes the previous months data back to the start of the FY. Responses are always dated the last day of the month they are received in.
The data looks like this:
+------------+---------+-----+-------+-------+-----------+
| Date | Account | Red | Amber | Green | Responses |
+------------+---------+-----+-------+-------+-----------+
| 30/04/2024 | 200 | 0 | 0 | 1 | 1 |
| 30/04/2024 | 200 | 0 | 0 | 1 | 1 |
| 30/04/2024 | 201 | 0 | 0 | 1 | 1 |
| 30/04/2024 | 201 | 1 | 0 | 0 | 1 |
| 31/05/2024 | 201 | 0 | 0 | 1 | 1 |
| 31/05/2024 | 201 | 0 | 0 | 1 | 1 |
| 31/05/2024 | 201 | 0 | 1 | 0 | 1 |
| 31/05/2024 | 201 | 1 | 0 | 0 | 1 |
| 31/05/2024 | 202 | 0 | 0 | 1 | 1 |
| 30/06/2024 | 200 | 0 | 0 | 1 | 1 |
| 30/06/2024 | 200 | 1 | 0 | 0 | 1 |
| 30/06/2024 | 200 | 0 | 1 | 0 | 1 |
| 30/06/2024 | 202 | 0 | 0 | 1 | 1 |
| 30/06/2024 | 202 | 0 | 0 | 1 | 1 |
+------------+---------+-----+-------+-------+-----------+
The result I'm looking to achieve is:
+------------+---------+-----------+---------------+--------+------------+
| Date | Account | Responses | Responses_YTD | Rating | Rating_YTD |
+------------+---------+-----------+---------------+--------+------------+
| 30/04/2024 | 200 | 2 | 2 | 100 | 100 |
| 30/04/2024 | 201 | 2 | 2 | 50 | 50 |
| 30/04/2024 | 202 | 0 | 0 | 0 | 0 |
| 31/05/2024 | 200 | 0 | 2 | 0 | 100 |
| 31/05/2024 | 201 | 4 | 6 | 25 | 16.7 |
| 31/05/2024 | 202 | 1 | 1 | 100 | 100 |
| 30/06/2024 | 200 | 3 | 5 | 50 | 40 |
| 30/06/2024 | 201 | 0 | 6 | 0 | 16.7 |
| 30/06/2024 | 202 | 2 | 3 | 100 | 100 |
+------------+---------+-----------+---------------+--------+------------+
Here to create the sample table & data:
CREATE TABLE #Ratings(
[Date] [date] NULL,
[Account] [varchar](10) NULL,
[Red] [int] NULL,
[Amber] [int] NULL,
[Green] [int] NULL,
[Responses] [int] NULL
)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-04-30' AS Date), N'200', 0, 0, 1, 1)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-04-30' AS Date), N'200', 0, 0, 1, 1)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-04-30' AS Date), N'201', 0, 0, 1, 1)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-04-30' AS Date), N'201', 1, 0, 0, 1)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-05-31' AS Date), N'201', 0, 0, 1, 1)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-05-31' AS Date), N'201', 0, 0, 1, 1)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-05-31' AS Date), N'201', 0, 1, 0, 1)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-05-31' AS Date), N'201', 1, 0, 0, 1)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-05-31' AS Date), N'202', 0, 0, 1, 1)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-06-30' AS Date), N'200', 0, 0, 1, 1)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-06-30' AS Date), N'200', 1, 0, 0, 1)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-06-30' AS Date), N'200', 0, 1, 0, 1)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-06-30' AS Date), N'202', 0, 0, 1, 1)
INSERT #Ratings ([Date], [Account], [Red], [Amber], [Green], [Responses]) VALUES (CAST(N'2024-06-30' AS Date), N'202', 0, 0, 1, 1)
Any assistance would be appreciated.
Many thanks
Martyn