Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 subquery without max()

Author  Topic 

workindan
Starting Member

21 Posts

Posted - 2011-10-26 : 13:18:26
I'm trying to join two tables and get the latest row on the second table without getting every row associated with a row on the first table. This is in Sybase, if that makes any difference.

SELECT
fName,lName,Address,
doctor_fname,doctor_lname, doctor_dateadded
FROM persons join doctors on persons.doctorID = doctors.DoctorID

Results:
Joe Bob 1234 Lane Emmit Smith 9/4/2011
Joe Bob 1234 Lane George George 8/20/2011
Joe Bob 1234 Lane Carol Carol 3/2/2011
Joe Bob 1234 Lane Dude Dude

as doctor info changes overtime, I get all the doctor records.
I could do a subquery with a max function, but that takes a really long time. Is there a smarter/faster way to do this?

X002548
Not Just a Number

15586 Posts

Posted - 2011-10-26 : 13:25:25
>> it takes a long time

Post the DDL including the Indexes...but...this is a sql server site

Try dbforums

However, if you are not properly indexed..it will scan..doesn't matter if you are Oracle, DB2, SQL Server, UDB, etc



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-27 : 03:02:12
quote:
Originally posted by workindan

I'm trying to join two tables and get the latest row on the second table without getting every row associated with a row on the first table. This is in Sybase, if that makes any difference.

SELECT
fName,lName,Address,
doctor_fname,doctor_lname, doctor_dateadded
FROM persons join doctors on persons.doctorID = doctors.DoctorID

Results:
Joe Bob 1234 Lane Emmit Smith 9/4/2011
Joe Bob 1234 Lane George George 8/20/2011
Joe Bob 1234 Lane Carol Carol 3/2/2011
Joe Bob 1234 Lane Dude Dude

as doctor info changes overtime, I get all the doctor records.
I could do a subquery with a max function, but that takes a really long time. Is there a smarter/faster way to do this?


why not take one record for each doctor and join to it?. You need to determine on what basis you want to retrieve single doctor record. is it first,last or random one

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-10-27 : 08:49:49
>> First, Last...?????


Huh?

Define First and Last

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-28 : 05:04:49
quote:
Originally posted by X002548

>> First, Last...?????


Huh?

Define First and Last

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/





that OP has to do. On what basis he needs to determine first or last occurance

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -