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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Hopefully an easy JOIN question...

Author  Topic 

Sschuster
Starting Member

26 Posts

Posted - 2005-09-12 : 16:41:38
I have three tables..
1) Accounts
2) Orders
3) OrdersProcessed

I need to come up with all the Orders records that have a matching Accounts record (based on AccountNumber) but do not have a matching OrdersProcessed record (based on OrderID and AccountNumber) and AccountNumber is like 'B%'. I tried the following query and it's not returning what I expect, it returns all the rows from Orders even though they have a matching record in the OrdersProcessed table. Any ideas?

SELECT O.AccountNumber, O.OrderID, O.Quantity, O.BuySell
FROM PFX_Orders O
FULL OUTER JOIN PFX_OrdersProcessed P ON O.AccountNumber = P.AccountNumber
AND O.OrderID = B.OrderID
INNER JOIN PFX_Accounts A ON A.AccountNumber = O.AccountNumber
WHERE O.AccountNumber like 'B%'

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-09-12 : 17:06:15
[code]
SELECT
O.AccountNumber,
O.OrderID,
O.Quantity,
O.BuySell
From PFX_Orders O
Inner Join PFX_Accounts A
On A.AccountNumber = O.AccountNumber
Left JOIN PFX_OrdersProcessed P
On O.AccountNumber = P.AccountNumber
and O.OrderID = B.OrderID
WHERE O.AccountNumber like 'B%'
and P.AccountNumber is null
[/code]

Corey

Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now."
Go to Top of Page

Sschuster
Starting Member

26 Posts

Posted - 2005-09-12 : 17:14:24
PERFECT! Exactly what I was looking for. Thanks!
Go to Top of Page
   

- Advertisement -