Solved

Include additional fields in an SQL ranking statement

Posted on 2007-12-06
2
344 Views
Last Modified: 2010-05-18
I have an SQL statement that an Experts Exchange expert constructed for me that works perfectly allowing me to output a ranking order listing the ranking number, the user name for that ranking, and the sum of miles for that user.
The results of that query is at http://www.lepirtle.com/lib/milelog/test18.cfm and that code is as follows:
SELECT myqry2.username, myqry2.SumOfmiles, (Select Count(*) from (SELECT tbl_mileusers.username, Sum(tbl_miles.miles) AS SumOfmiles
FROM tbl_mileusers
INNER JOIN tbl_miles ON tbl_mileusers.userkey = tbl_miles.userkey
GROUP BY tbl_mileusers.username)
WHERE [Sumofmiles]>[myqry2].[SumOfmiles])+1 AS Ranking
FROM (SELECT tbl_mileusers.username, Sum(tbl_miles.miles) AS SumOfmiles
FROM tbl_mileusers
INNER JOIN tbl_miles ON tbl_mileusers.userkey = tbl_miles.userkey
GROUP BY tbl_mileusers.username) AS myqry2
ORDER BY myqry2.SumOfmiles DESC;

However,  I would like to add the users town and state to that output but haven't been successful in modifying the SQL statement. The two fields that I want to output are stored in the tbl_mileusers field and are named tbl_mileusers.town, and tbl_miles.st. I have experimented with the following statement:
SELECT myqry2.username, myqry2.SumOfmiles, tbl_mileusers.town, tbl_mileusers.st, (Select Count(*) from (SELECT tbl_mileusers.username, Sum(tbl_miles.miles) AS SumOfmiles
FROM tbl_mileusers
INNER JOIN tbl_miles ON tbl_mileusers.userkey = tbl_miles.userkey
GROUP BY tbl_mileusers.username, tbl_mileusers.town, tbl_mileusers.st)
WHERE [Sumofmiles]>[myqry2].[SumOfmiles])+0 AS Ranking
      FROM
            (SELECT tbl_mileusers.username, tbl_mileusers.town, tbl_mileusers.st, Sum(tbl_miles.miles) AS SumOfmiles
            FROM tbl_mileusers
            INNER JOIN tbl_miles ON tbl_mileusers.userkey = tbl_miles.userkey
            WHERE year(tbl_miles.milesdate) = Year(Date())
            GROUP BY tbl_mileusers.username,tbl_mileusers.town, tbl_mileusers.st) AS myqry2
ORDER BY myqry2.SumOfmiles DESC;
However as you can see from the results at http://www.lepirtle.com/lib/milelog/test19.cfm, the "ranking" number is erroneous.
Can someone please correct my error?
Thanks.

0
Comment
Question by:lepirtle
2 Comments
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20424444
SELECT myqry2.username, myqry2.town, myqry2.st, myqry2.SumOfmiles, (
      Select Count(*) from (
            SELECT tbl_mileusers.username, Sum(tbl_miles.miles) AS SumOfmiles
            FROM tbl_mileusers
            INNER JOIN tbl_miles ON tbl_mileusers.userkey = tbl_miles.userkey
            GROUP BY tbl_mileusers.username) AS X
      WHERE [Sumofmiles]>[myqry2].[SumOfmiles]) +1 AS Ranking
FROM (
      SELECT tbl_mileusers.username, tbl_mileusers.town, tbl_mileusers.st, Sum(tbl_miles.miles) AS SumOfmiles
      FROM tbl_mileusers
      INNER JOIN tbl_miles ON tbl_mileusers.userkey = tbl_miles.userkey
      GROUP BY tbl_mileusers.username, tbl_mileusers.town, tbl_mileusers.st) AS myqry2
ORDER BY myqry2.SumOfmiles DESC;

0
 

Author Comment

by:lepirtle
ID: 20424502
You nailed it!!
Thank you so very much.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
oracle query help 18 78
Select2 jquery help 9 46
PL/SQL - Leading zeros 7 41
Access Date Query 28 29
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

12 Experts available now in Live!

Get 1:1 Help Now