Solved

Selecting from one table, ordering according to other one

Posted on 2008-06-17
5
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 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 28

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

687 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