Visual Studio SQL Join

Having trouble with a 1 to many join in a sql query.
Seems its only returning PortfolioCodes when it finds a TradeDate.
How can I get all PortfolioCodes to show even if it doesn't have a TradeDate?

SELECT     AdvApp.vPortfolio.PortfolioCode, AdvApp.vPortfolioTransaction.TradeDate
FROM         AdvApp.vPortfolio LEFT OUTER JOIN
                      AdvApp.vPortfolioTransaction ON AdvApp.vPortfolio.PortfolioID = AdvApp.vPortfolioTransaction.PortfolioID
WHERE     (AdvApp.vPortfolioTransaction.TradeDate = '9/25/13')
ShawnGrayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
Since it's a left join it's possible there could be no transactions, therefore you need to include the possibility of null
SELECT     AdvApp.vPortfolio.PortfolioCode, AdvApp.vPortfolioTransaction.TradeDate
FROM         AdvApp.vPortfolio LEFT OUTER JOIN
                      AdvApp.vPortfolioTransaction ON AdvApp.vPortfolio.PortfolioID = AdvApp.vPortfolioTransaction.PortfolioID
WHERE     (AdvApp.vPortfolioTransaction.TradeDate = '9/25/13' or AdvApp.vPortfolioTransaction is null)

Open in new window

0
TempDBACommented:
SELECT    
             AdvApp.vPortfolio.PortfolioCode, AdvApp.vPortfolioTransaction.TradeDate
FROM        
             AdvApp.vPortfolio LEFT OUTER JOIN AdvApp.vPortfolioTransaction
             ON AdvApp.vPortfolio.PortfolioID = AdvApp.vPortfolioTransaction.PortfolioID
             AND AdvApp.vPortfolioTransaction.TradeDate = '9/25/13'


Put that condition in join condition rather then where condition, When you do where, it filters out the data where it doesn't match.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony PerkinsCommented:
As TempDBA has alluded to (no points please), by adding that condition you have essentially converted the query to an implicit INNER JOIN.

Also, you need to get used to using aliases, it makes it easier to read for you and everyone else.  As in:
SELECT  p.PortfolioCode,
        t.TradeDate
FROM    AdvApp.vPortfolio p
        LEFT OUTER JOIN AdvApp.vPortfolioTransaction t ON p.PortfolioID = t.PortfolioID
                                                          AND t.TradeDate = '20130925'

Open in new window

0
ShawnGrayAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SSRS

From novice to tech pro — start learning today.