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
 Select all from one table JOIN

Author  Topic 

pvong
Yak Posting Veteran

58 Posts

Posted - 2012-10-15 : 22:48:53
I don't know what I'm doing wrong. Here is my Setup

ClientsOnePercent
-----------------
Autex
OnePctTypeID

AxysPals
--------
Autex
Quantity
SecType
Ticker

I want to show ALL rows from ClientsOnePercent where the OnePctTypeID = 1. I want to join it to the AxysPals table and show all Quantities that exist with each Autex where SecType = csus and Ticker = c. Not all Autex will have that sectype or ticker and I want it to show nothing if it does not exist in AxysPal. I thought a simple Left Outer Join would do the trick.

Here is my Select:

SELECT        ClientsOnePercent.Autex, ClientsOnePercent.OnePctTypeID, AxysPals.Quantity
FROM ClientsOnePercent LEFT OUTER JOIN
AxysPals ON ClientsOnePercent.Autex = AxysPals.Autex
WHERE (ClientsOnePercent.OnePctTypeID = 1) AND (AxysPals.SecType = 'csus') AND (AxysPals.Ticker = 'c')


This does not give me all Autex from the ClientsOnePercent table. It will only shows me the ones that have the where clause.

Thanks in advance.

Phil


------------------------------
Using VS2010 / Learning in VB.Net / Win2008 R2 / SQL 2008 R2
Be kind to the newbies because you were once there.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-15 : 22:54:11
it should be this


SELECT ClientsOnePercent.Autex, ClientsOnePercent.OnePctTypeID, AxysPals.Quantity
FROM ClientsOnePercent LEFT OUTER JOIN
AxysPals ON ClientsOnePercent.Autex = AxysPals.Autex
AND (AxysPals.SecType = 'csus') AND (AxysPals.Ticker = 'c')
WHERE (ClientsOnePercent.OnePctTypeID = 1)


reason is putting columns from right table in where on a left join query causes it to reduce to INNER JOIN

see illustration here

http://weblogs.sqlteam.com/jeffs/archive/2007/05/14/criteria-on-outer-joined-tables.aspx

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

Go to Top of Page

pvong
Yak Posting Veteran

58 Posts

Posted - 2012-10-16 : 10:50:00
Thanks you so much!!! This worked and that article was extremely helpful. I don't think I will ever ask this question again thanks to that article.

------------------------------
Using VS2010 / Learning in VB.Net / Win2008 R2 / SQL 2008 R2
Be kind to the newbies because you were once there.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-16 : 23:13:32
welcome

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

Go to Top of Page
   

- Advertisement -