tengoTengo el siguiente query sql y deseo conocer cualescuáles son los 3 service masmás utilizados por cada customer, mostrando de mayor a menor los service masmás utilizados por el customer, asiasí como la cantidad de veces que utilizo el service:
WITH cte_customer_usage AS (
SELECT
customer,
service,
COUNT(*) OVER (PARTITION BY customer ORDER BY service DESC) AS usage_count,
ROW_NUMBER() OVER (PARTITION BY customer ORDER BY usage_count DESC) AS usage_rank
FROM total_service
GROUP BY customer, service
)
SELECT
cte_customer_usage.customer,
cte_customer_usage.service,
cte_customer_usage.usage_count
FROM cte_customer_usage
WHERE usage_rank <= 3
ORDER BY customer, usage_rank DESC
pero me da el siguiente error:
Msg 207, Level 16, State 1, Line 6 Invalid column name 'usage_count'.
Msg 207, Level 16, State 1, Line 6 Invalid column name 'usage_count'.