Solved

Calculate a user's position in a leader board

Posted on 2008-06-18
1
249 Views
Last Modified: 2010-03-20
Hello

I have designed a league table which calculates points based on how many virtual cups of tea are given and received. 1 point for a given cup, 3 for a received.

A variable @I counts the position that the user ranks in the league, and it gives a Top 10.

How would I go about retrieving a given users position in the league, assuming most users will be outside the top ten? If a WHERE clause is used, the @I counter does not get to count the correct position in the league, and simply shows 1, as 1 record is returned.

Is a self-join needed...somehow?

Thanks!
+---------+---------------+-------------+---------------------+--------------+

| GIVE_ID | GIVE_UID_FROM | GIVE_UID_TO | GIVE_DATETIME       | GIVE_MESSAGE |

+---------+---------------+-------------+---------------------+--------------+

|       2 |     507426179 |   502129134 | 2008-05-02 17:16:44 | NULL         |

|       3 |     508580684 |   507426179 | 2008-05-02 17:21:31 | NULL         |

|       4 |     507426179 |   835984465 | 2008-05-02 17:23:39 | NULL         |

|       5 |     502129134 |   507426179 | 2008-05-02 17:23:45 | NULL         |

|       6 |     507426179 |   285100389 | 2008-05-02 17:23:46 | NULL         |

|       7 |     502129134 |   507426179 | 2008-05-02 17:24:56 | NULL         |

|       8 |     507426179 |   726170211 | 2008-05-02 17:29:14 | NULL         |

|       9 |     507426179 |   514816730 | 2008-05-02 17:29:54 | NULL         |

|      10 |     507426179 |   285100389 | 2008-05-02 17:47:33 | NULL         |

|      11 |     507426179 |   835984465 | 2008-05-02 18:00:00 | NULL         |

+---------+---------------+-------------+---------------------+--------------+
 
 

SET @I := 0;
 
 

		SELECT `GIVE_UID_FROM` , 

	

		( 	

			SELECT COUNT( * ) 

			FROM GIVE AS T 

			WHERE T.GIVE_UID_TO = SQ.GIVE_UID_FROM 

		)  	AS GIVE_TOTAL_RX,  	

	

		COUNT( `GIVE_ID` ) AS GIVE_TOTAL_TX,  	

	

		( 	

			SELECT (GIVE_TOTAL_RX *3) + COUNT( `GIVE_ID` ) 

		)  	AS GIVE_TOTAL_POINTS,

	

		@I := @I + 1 AS `GIVE_POSITION`
 

		FROM GIVE AS SQ

		GROUP BY `GIVE_UID_FROM`

		ORDER BY `GIVE_TOTAL_POINTS` DESC, `GIVE_TOTAL_RX` DESC

	LIMIT 10

Open in new window

0
Comment
Question by:matthewhamer
1 Comment
 

Accepted Solution

by:
matthewhamer earned 0 total points
ID: 21818609
I figured it out: Use a temporary table to store the results. Then query the temporary table.

Simply proceed the Select query with the following to generate an in-memory table which will be automatically destroyed when the connections drops. Perfect.
CREATE TEMPORARY TABLE LEAGUE ENGINE="MEMORY"

Open in new window

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read about achieving the basic levels of HRIS security in the workplace.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now