mysql - retrieve unique ID from aggregate function MAX/GROUP BY -


i'm getting error when using aggregate function. guys can me, explained situation below.

in football/soccer game player gets red card after second yellow card. query qry1twiceyellow gives unique id of players have 2 yellow cards. query written down below , works fine.

select player_id (select player_id, count(id) total yellowcards game_id=29 , homeoraway=1 group player_id) total=2;

now want retrieve information last card:

  • the unique id of last yellow card.
  • minute offence happened.
  • the player id.

i wrote following query causes error (below query).

select y.id, y.player_id, max(y.minute) lastyellow qry1twiceyellow q inner join yellowcards y on q.player_id = y.player_id y.game_id=29 , y.homeoraway=1 group y.player_id;

your query not include specified expression 'id' part of aggregate function.

i tried following solutions:

  • by adding id group error disappears max(minute) aggregate function useless since id unique.
  • leaving out unique id. inner join player_id , max(minute) complete table unique id. works fine, until 2 or more players in same minute there second yellow card.

how solve this?

thnx in advance, chris

here's query selects 2nd yellow cards game counting number of previous cards same game , player:

select y1.id, y1.player_id, y1.minute yellowcards y1 y1.game_id=29 , y1.homeoraway=1  , (     select count(*)     yellowcards y2     y1.game_id = y2.game_id , y1.homeoraway = y2.homeoraway , y1.player_id = y2.player_id , y1.minute > y2.minute ) = 1; 

here's sqlfiddle


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -