Jump to content

Recommended Posts

Firstly, I will post the query

 

SELECT ts_game_league.game_id, ts_game_league.league, MAX(ts_table.week), ts_table.user_id, MAX(ts_table.total_points) FROM ts_game_league LEFT JOIN ts_table ON ts_game_league.game_id = ts_table.game_id AND ts_game_league.league = ts_table.league GROUP BY ts_game_league.game_id, ts_game_league.league

 

Next, this is the results it gives me:

 

game_id 	league 	week 	user_id 	MAX( ts_table.total_points )
1 	1 	39 	78 	415
2 	1 	1 	1 	50

 

This works, except the user_id is not what I would want. Basically, this query should return the maximum total points for the last week (which is essentially an incrementing number) and the user that got the maximum points for each game and league. I know the query is wrong, but I cannot think of a way to do what I want in one query, but sure it must be possible some how?

 

A bit of an explanation of the fields above:

 

  • game_id - part of composite key (with league)
  • league - part of composite key (with game_id)
  • week - an incrementing number that shows the week of the game/league
  • user_id - a foreign key to identifier of user that got the score
  • total_points - the total points the user got for the game/league/week

 

If anyone could offer any advice I would be most grateful. 

Link to comment
https://forums.phpfreaks.com/topic/82405-help-with-query-please/
Share on other sites

ts_game_league

============

game_id (PK)

league (PK)

 

ts_table

======

week

user_id

total_points

game_id (FK from ts_game_league)

league (FK from ts_game_league)

 

I need:

- game_id

- league

- maximum week for each game_id/league key

- total_points for each game_id/league key

- user_id of user that got the total_points

 

Example:

game_id - 1

league - 2

week - 10

total_points - 50

user_id - 24

 

game_id - 1

league - 3

week - 8

total_points - 55

user_id - 20

 

game_id - 2

league - 1

week - 10

total_points - 65

user_id - 22

 

Thanks again

OK... so run

 

select game_id, league, max(total_points) as maxpoints

from ts_table

group by game_id, league

 

and then join this result back to ts_table with all three of these columns as the on condition... then the users table should be another join.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.