[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Selecting from one table, ordering according to other one

Posted on 2008-06-17
5
Medium Priority
?
200 Views
Last Modified: 2010-04-21
tbl_rides: ID | more_fields
tbl_results: rideID | rank | more_fields

SELECT * FROM tbl_rides WHERE ID IN (SELECT rideID FROM tbl_results)

I need to order the above statement according to tbl_results.rank field desc. (Higher rank appears top). How is it done?

Using MS ACCESS
0
Comment
Question by:shmulik15
5 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 2000 total points
ID: 21804626
try this


SELECT tbl_rides.* , tbl_results.rank
FROM tbl_rides
inner join tbl_results on
tbl_rides.ID=tbl_results.rideID
order by tbl_results.rank desc
0
 
LVL 29

Expert Comment

by:Bill Bach
ID: 21804672
With SQL, you are dealing with "unordered sets" of data.  Therefore, when you build the set of data for the inner SELECT statement, order is meaningless.

To get the data exactly in the order you need, you must join the two sets first, then apply the order by, and then select out your fields.  It would look like this:

SELECT tbl_rides.*
FROM tbl_rides
INNER JOIN tbl_results ON tbl_rides.ID = tbl_results.rideID
ORDER BY tbl_results.rank

Please note that this may NOT give you back the exact same data set, as it may duplicate some data, you you may need to add a DISTINCT clause to it, but this will get you close.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 21810090
If you want only those rows in tbl_rides where an ID exists in tbl_results, then an inner join is the way to go. Otherwise you may want to consider a left outer join...

and just realised I type the same code as capricorn1 for the inner join...

SELECT tbl_rides.* , tbl_results.rank
FROM tbl_rides
inner join tbl_results on tbl_rides.ID=tbl_results.rideID
order by rank desc

so, the outer join, putting unranked rides at the bottom...

SELECT tbl_rides.* , iif( isnull(tbl_results.rank),0,tbl_results.rank) as RANK
FROM tbl_rides
left outer join tbl_results on tbl_rides.ID=tbl_results.rideID
order by rank desc
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21810302
just for the "fun", you CAN of course do it without a "join" :)
SELECT r.* 
FROM tbl_rides r
WHERE r.ID IN (SELECT rideID FROM tbl_results)
ORDER BY (SELECT TOP 1 rank FROM tbl_results re WHERE re.rideID = r.ID )
 
resp with a exists:
 
SELECT r.* 
FROM tbl_rides r
WHERE EXISTS( SELECT null FROM tbl_results re WHERE re.rideID = r.ID )
ORDER BY (SELECT TOP 1 rank FROM tbl_results re WHERE re.rideID = r.ID )

Open in new window

0
 

Author Closing Comment

by:shmulik15
ID: 31468013
Thanks a lot, your help is appreciated.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

834 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