Solved

Include additional fields in an SQL ranking statement

Posted on 2007-12-06
2
346 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql server insert 13 36
SQL Server 2012 r2 - Sum totals 2 28
sql query to calculate avaerage 21 52
Separate 2 comma delimited columns into separate rows 2 41
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 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