Skip to content
Advertisement

SQL Max count for each group

I’m having trouble calculating a max count for each value in a column (Year_month). The business question is: for each Year_month, which Week_day has the highest number of records.

Trying to list the Week_day for each Year_month that has the highest record count. I thought I’d create a view to aggregate (count) and then to get the max from these counts for every Year_month, but I have not been able to achieve this. I have had a look at many ‘similar’ posts and tried their code, but none has produced the right answer.

What the Data looks like:

What the Data looks like

SQL So Far:

What I am getting currently

What I am getting currently

What I’d like to end up with is something like this:

What I'd like to end up with is something like this

Any help would be most appreciated. Thank you.

Advertisement

Answer

After determining the number of records for each Year_month,Week_day, you may use the ROW_NUMBER function to determine the order by the number of records in each Year_month partition group. We can then filter based on the order to retrieve the top record.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement