Skip to main content
Removed the comma that directly followed the alias 'frequency' in the SELECT statement
Source Link
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;DESC ; 

The second part of the order by needs to be customerID just in case of ties. IfI assume that if you have two or more customers with the same number of events, then I assume you don't want their events mixed up.

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; 

The second part of the order by needs to be customerID just in case of ties. If you have two or more customers with the same number of events, then I assume you don't want their events mixed up.

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 ; 

The second part of the order by needs to be customerID just in case of ties. I assume that if you have two or more customers with the same number of events, you don't want their events mixed up.

Removed the comma that directly followed the alias 'frequency' in the SELECT statement
Source Link

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 ;DESC; 

The second part of the order by needs to be customerID just in case of ties. If you have two or more customers with the same number of events, then I assume you don't want their events mixed up.

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 ; 

The second part of the order by needs to be customerID just in case of ties. If you have two or more customers with the same number of events, I assume you don't want their events mixed up.

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; 

The second part of the order by needs to be customerID just in case of ties. If you have two or more customers with the same number of events, then I assume you don't want their events mixed up.

added 197 characters in body
Source Link
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 ; 

The second part of the order by needs to be customerID just in case of ties. If you have two or more customers with the same number of events, I assume you don't want their events mixed up.

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 ; 

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 ; 

The second part of the order by needs to be customerID just in case of ties. If you have two or more customers with the same number of events, I assume you don't want their events mixed up.

Source Link
ypercubeᵀᴹ
  • 98.6k
  • 13
  • 215
  • 305
Loading