Skip to content
Advertisement

Apply limit to CASE statement

I am trying to collect counts of different columns for players (you don’t need to know the ins and outs of all the columns). We have a player_1 and player_2 column so I have made it distinct to get total counts, as you can see below:

I want to also get the frequency of goals for last 32 games for those players as well.. There isn’t a way to add a LIMIT clause to the CASE WHEN I’m guessing?

E.g, I want to add a column:

This won’t work, I’m guessing because the table we are selecting from contains all rows and the CASE WHEN statement won’t allow the limits to apply?

I could do this in a separate query, but ideally I want it all in one.

Help appreciated.

Advertisement

Answer

I want to also get the frequency of goals for last 32 games for those players as well.

You will need an additional subquery and then conditional aggregation:

Notes:

  • ORDER BY in a derived table subquery is really a no-op. It is superfluous, so I removed it.
  • You can simplify your COUNT() columns just by adding the boolean value. This is a very convenient MySQL extension.
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement