Skip to main content
1 of 4
ypercubeᵀᴹ
  • 98.6k
  • 13
  • 215
  • 305

It seems you don't really need the "frequency" for the sorting, only the "maximum frequency" per customer. You can use the COUNT(*) OVER (PARTITION BY customerID):

SELECT
    customerID, eventID,
    ROW_NUMBER() OVER (PARTITION BY customerID ORDER BY eventID) AS frequency,  
FROM 
    customerEvent
ORDER BY 
    COUNT(*) OVER (PARTITION BY customerID) DESC,
    customerID,
    frequency DESC ; 
ypercubeᵀᴹ
  • 98.6k
  • 13
  • 215
  • 305