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
 Pulling the first from a multiples during Join

Author  Topic 

JohnGault777
Starting Member

31 Posts

Posted - 2011-02-25 : 14:19:45
Sorry if the title isn't very descriptive but I didn't know how to write it.

I want to pull only the first entry from a table where the PK / FK match.

I have a table showing customers that links to a table showing locations. The locations are listed 1, 2, 3, 4,...etc.. The locations have a territory ID. I want to pull the Territory ID for the 1st location only (it is typically the primary location).

So when the PK in Table 1 equal the FK in Table 2 and the location ID is 1 pull the Territory. What is the syntax to write this kind of statement?

JG777

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-02-25 : 14:41:14
yes its not descriptive, but never mind Can you come up with sample data of your table(s), PK, FKs of those tables and The Desired Output your are looking for. this will help us to guide you accordingly.

Cheers
MIK
Go to Top of Page

JohnGault777
Starting Member

31 Posts

Posted - 2011-02-25 : 15:32:21
Ok here are the necessary tables I'm working with.

dbo.Auto (example: 2,000 rows)
-------------
autoID (PK)

dbo.Vehicle (example: 5,000 rows)
-------------
vehicleID (PK)
autoID (FK)
vehicleNumber (1, 2, 3, 4,...etc. for each AutoID)
Territory


I know it could do an inner join to get all the Territories, but it would take the AutoID and duplicate it for every different vehicle. Therefore, the output would be 5,000 rows; one for each vehicle.

I want the output to only be 2,000 rows and only return the Territory for the first vehicleNumber. I know the description is different from the previous post but I was trying to simplify it.

How do I write the statement to say "When the AutoID from the Auto table equals the AutoID in the Vehicle table and the vehiclenumber is 1 pull the territory."?

Is this enough information?

JG777
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-02-25 : 15:33:54
Since the physical order of data in a database has no meaning

define "first"



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

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2011-02-25 : 15:44:42
Based on this:
"When the AutoID from the Auto table equals the AutoID in the Vehicle table and the vehiclenumber is 1 pull the territory."

SELECT v.Territory
FROM Vehicle v
JOIN Auto a ON v.AutoID = a.AutoID
AND vehicleNumber = 1



Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

JohnGault777
Starting Member

31 Posts

Posted - 2011-02-25 : 16:13:17
Brett - I know physical order has no meaning. I guess my explanation wasn't clear. vehicleNumber is literally counting up: 1, 2, 3, 4, 5. So the first vehicle is 1, the second vehicle is 2....etc. I apologize for not posting the question correctly. I have literally been shoved into working with SQL. I've only been working with it for about 5 weeks now. I can barely understand your hint link even though it is probably very basic (I'm struggling with basic right now). Thank you for the other links. I'm very interested in the SQL Server Analysis Services. I've been reading some of that in the last little bit.

Skorch - Using that syntax worked perfectly. Thank you.

Thank you to all for baring with me and helping me learn this stuff. The old saying "you learning something new everyday." doesn't apply to me right now. I'm learning 1,000 new things each day simply by necessity.



JG777
Go to Top of Page

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2011-02-25 : 16:28:41
You're very welcome. Glad it worked out :)

Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page
   

- Advertisement -