Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2004, 04:08 AM
Authorized User
 
Join Date: Nov 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by spinout
 Hi There
I am using a SQL statement that beings with SELECT TOP 1 and also has an INNER JOIN.

Basically, I only want to select the first row from the first table but this table has many records associated with it in the second table and I only get the first row returned for the second table as well.

I want to be able to get all of the associated rows from the second table that match the top row I select from the first. How would I achieve this?

Thanks for your help :)


How about this:
SELECT *
FROM child_table
WHERE child_table.key=(SELECT TOP 1 primary_key
                       FROM parent_table);

it seem that subquery is required for your situation

 
Old November 16th, 2004, 04:14 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Yes that worked. However it only returned all the fields from the child table that matched the top row ID column in the primary table. I need to be able to return multiple columns in the top row of the primary table rather than just the ID field.

I tried doing this by modifying the query but I got an error message saying "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."

 
Old November 16th, 2004, 04:28 AM
Authorized User
 
Join Date: Nov 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by spinout
 Yes that worked. However it only returned all the fields from the child table that matched the top row ID column in the primary table. I need to be able to return multiple columns in the top row of the primary table rather than just the ID field.

I tried doing this by modifying the query but I got an error message saying "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."

How about this
SELECT *
FROM child_table, parent_table
WHERE foreign_key=primary_key AND primary_key=(SELECT top 1 primary_key FROM parent_table);

A modified version of my suggestion.....
but the performance isn't as fast as before

 
Old November 16th, 2004, 04:40 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

That worked!! Thanks :)

 
Old November 22nd, 2004, 08:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi spinout,

You could have just added this to the query I posted earlier.
Code:
Select top 1 a.*, b.*
from table1 a, table2 b
where a.idcolumn=b.idcolumn
order by the relavant_column
OR
Code:
Select top 1 * 
from table1 a, table2 b
where a.idcolumn=b.idcolumn
order by the relavant_column
Cheers!

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
SELECT TOP n rgerald SQL Server 2000 3 May 12th, 2006 04:03 PM
SELECT TOP n NOT SELECTING TOP n! ibi SQL Language 8 March 30th, 2005 08:08 PM
Join Query Distinct and Top 1 ahanson SQL Language 4 November 30th, 2004 01:29 PM
SELECT TOP FROM HAVING khatfield29 SQL Language 1 August 23rd, 2004 02:41 PM
Need help with TOP 5 and self join funkedup SQL Language 2 May 17th, 2004 11:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.