SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select all from one table JOIN
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pvong
Yak Posting Veteran

52 Posts

Posted - 10/15/2012 :  22:48:53  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 10/15/2012 :  22:54:11  Show Profile  Reply with Quote
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

52 Posts

Posted - 10/16/2012 :  10:50:00  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 10/16/2012 :  23:13:32  Show Profile  Reply with Quote
welcome

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000