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.
| Author |
Topic |
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2009-03-02 : 22:18:38
|
| hiI have previously posted a similar threadhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=119353and i thought i was working well until the PK values is different.Ok, this is what i have: Notice that the ID of both tables are different.tblBuyID, ProductType, Locations, BuyerName, UserID1 A West John 12 B West Kent 13 B West Sally 1tblSellID, ProductType, Locations, SellerName, UserID1001 A West Clark 11002 B West Jim 11003 B West Kim 1and using the below SQL:SELECT 'Buyer' AS [Buyer/Seller],b.BuyerName,b.ProductType,b.Locations,COALESCE(s.ID,0) AS MatchFROM tblBuy bOUTER APPLY (SELECT TOP 1 ID FROM tblSell WHERE ProductType=b.ProductType AND Locations=b.Locations ORDER BY ID) sWHERE b.UserID=1UNION ALLSELECT 'Seller' AS [Buyer/Seller],s.SellerName,s.ProductType,s.Locations,COALESCE(b.ID,0) AS MatchFROM tblSell sOUTER APPLY (SELECT TOP 1 ID FROM tblBuy WHERE ProductType=s.ProductType AND Locations=s.Locations ORDER BY ID) bWHERE s.UserID=1The criteria for matching is by both ProductType and Location so when John the buyer from tblBuy wants to buy product "A" from and from location "West", it would try to match tblSell both ProductType and Location and the reults is 1 and for buyer Kent the result match would be 2. And seller Kim from tblSell would also try to match both ProductType and Location with tblBuy, and the result is 2. Now, all these results are based on logon UserID which is 1. Also, would like to display a 0 when there isn't a match meaning someone wants to buy but nobody sells or vice-versa, so it would display a 0. How should i go about it? Thanks |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2009-03-03 : 07:45:05
|
| hiOk, I got it working. Thanks |
 |
|
|
|
|
|
|
|