Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

selecting the result of a calculation from one column

Status
Not open for further replies.

Silicor1

IS-IT--Management
Aug 4, 2005
1
US
mysql 4.1.11

I have hit a query that I cannot make and would be really appreciative of some help. I was thinking that a derived field would be the right approach, but I can't seem to get it working.

TABLE/COLUMNS
Table: Rating
Columns: statperiod, player, totalwins

DATA
1, Bill, 5
1, Todd, 1
2, Bill 11
2, Todd, 4

I need a query that will give me the player with the most wins for the current period. The total wins culumn are the cummulative wins for the player. For example, if the current period is 2:

OUTPUT

Bill, 6 (periodwins=period 2 wins minus period 1 wins, Todd only had 3 wins in the period so Bill had the most)
 
How about:
[tt]
SELECT
player,
SUM(
IF(statperiod=$currentperiod,totalwins,0-totalwins)
)
wins
FROM rating
WHERE
statperiod BETWEEN $currentperiod-1 AND $currentperiod
GROUP BY player
ORDER BY wins DESC
LIMIT 1
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top