That works when all the records from the CTE is based on one client. Now, I have added more records in the CTE coming from various clients. In my case, I need the get the count based on a condition for one specific client.
For example, lets say I have grouped by months the number of sales for each client. This could be the result of the CTE :
2019-06-01 ClientA 1
2019-07-01 ClientA 2
2019-08-01 ClientA 4
2019-09-01 ClientA 0
2019-10-01 ClientA 6
2019-06-01 ClientB 2
2019-07-01 ClientB 2
2019-08-01 ClientB 3
2019-09-01 ClientB 1
2019-10-01 ClientB 2
In the subquery, one of the formulas needs to divide the sum of all the sales for a client by the count of the number of records for that client. Thus, ClientA needs to divide 13 by 5 because 1+2+4+0+6 equals 13 and we have 5 records from ClientA in the CTE.
Any proper approach to suggest?