| Author |
Topic |
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2009-02-06 : 04:37:34
|
| hiI have 2 tables namely: tblBuy(Field: ID,BuyType,userid) and tblSell (Field: ID,SellType,userid)Basically. Users that post to buy would match those that post to sell later and vice-versaFor example, User A entered into tblBuy to buy Type "A" and later User B entered into tblSell Type "A" and later User C entered into tblSell Type "A"As user A logon to the system, the first thing he would see is this:Buy MatchA 2or if nothing then Buy MatchA 0The same is also true when User D entered into tblSell Type "B" and later Users E and F entered into tblBuy Type "B"As user D logon to the system, the first thing he would see is this:Sell MatchB 2or if nothing then Sell MatchB 0How should it go about it? Thanks |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-06 : 04:58:05
|
| check this is ur required querydeclare @tblBuy table( ID int,BuyType varchar(6),userid int)insert into @tblbuy select 1,'a',1 union allselect 2,'b',2declare @tblSell table ( ID int,SellType varchar(32),userid int)insert into @tblsellselect 1,'b',1 union allselect 2,'c', 1 union allselect 3,'e',2 union allselect 4,'f',2 union allselect 5,'g',2select distinct b.buytype, cnt from @tblbuy b inner join (select count(userid)as cnt,userid from @tblsell group by userid) s on s.userid = b.userid |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-06 : 05:04:41
|
[code]SELECT b.BuyType AS theType, b.UserID AS Buyer, s.UserID AS Seller, COUNT(*)FROM tblBuy AS bFULL JOIN tblSell AS s ON s.SellType = b.BuyTypeWHERE @UserID IN (b.UserID, s.UserID)GROUP BY b.BuyType, b.UserID, s.UserID[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-06 : 05:04:49
|
quote: Originally posted by Peso
SELECT b.UserID, b.BuyType, s.UserID, s.SellTypeFROM tblBuy AS bFULL JOIN tblSell AS s ON s.SellType = b.BuyTypeWHERE @UserID IN (b.UserID, s.UserID) E 12°55'05.63"N 56°04'39.26"
hai peso output astype, match (count of match records) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-06 : 05:07:15
|
Yes, that's why I removed it and posted a better solution. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-06 : 05:18:40
|
[code]SELECT ProductType, MAX(PartnerType) AS PartnerType, COUNT(DISTINCT Partners) AS PartnersFROM ( SELECT b.BuyType AS ProductType, CASE b.UserID WHEN @UserID THEN 'I buy' ELSE 'I sell' END AS PartnerType, CASE b.UserID WHEN @UserID THEN s.UserID ELSE b.UserID END AS Partners FROM tblBuy AS b FULL JOIN tblSell AS s ON s.SellType = b.BuyType WHERE @UserID IN (b.UserID, s.UserID) ) AS dGROUP BY ProductTypeORDER BY ProductType[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-06 : 05:22:28
|
[code]SELECT 'I sell' AS Header, b.BuyType AS ProductType, COUNT(DISTINCT b.UserID) AS PartnersFROM tblBuy AS bINNER JOIN tblSell AS s ON s.SellType = b.BuyTypeWHERE s.UserID = @UserIDGROUP BY b.BuyTypeUNION ALLSELECT 'I buy' AS Header, s.SellType AS ProductType, COUNT(DISTINCT s.UserID) AS PartnersFROM tblSell AS sINNER JOIN tblBuy AS b ON b.BuyType = s.SellTypeWHERE b.UserID = @UserIDGROUP BY s.SellType[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2009-02-06 : 11:40:05
|
| Thank you so much. I will do some runs on the actual data. |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2009-02-07 : 10:54:03
|
| hiOk, it seems that there is a little changes here to the table schemas.Now, i have 2 tables namely tblSell and tblBuy and the fields for both table aretblBuyID, ProductType, Locations, BuyerName, UserID1 A West John 12 B West Kent 13 B West Sally 3tblSellID, ProductType, Locations, SellerName, UserID1 A West Clark 22 B West Jim 23 B West Kim 1Now if i logon as UserID 1 then the result should match by both ProductType and location. And the final result would look like this:Buyer/Seller ProductType Location MatchBuyer John A West 1Buyer Kent B West 2Seller Kim B West 2How should i go about it. I did try to tweak the given solution but to no avail. I am sorry for the inconvenience caused. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-07 : 11:52:14
|
| where does Match value come from? |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2009-02-07 : 12:09:44
|
| hiIt come from both tables. For example, because the 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. Thanks |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2009-02-07 : 12:14:14
|
| In fact, if there is a need to change the DB design to get the results. Is fine with me, as long as it produce the result set. Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-07 : 14:02:58
|
| [code]SELECT 'Buyer' AS [Buyer/Seller],b.BuyerName,b.ProductType,b.Locations,s.ID AS MatchFROM tblBuy bCROSS 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,b.ID AS MatchFROM tblSell sCROSS APPLY (SELECT TOP 1 ID FROM tblBuy WHERE ProductType=s.ProductType AND Locations=s.Locations ORDER BY ID) bWHERE s.UserID=1[/code] |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2009-02-07 : 20:51:06
|
| Thanks so much. It works wonderful. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-08 : 01:18:48
|
welcome |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2009-02-08 : 12:16:31
|
| Very sorry. One last question. I wish to display a 0 match.For example, based on the data given, all seems to have a match. I 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. Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-08 : 21:16:29
|
modify like below 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=1 |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2009-02-08 : 21:32:21
|
| Thanks, It work fine now. Thanks so much again. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-08 : 21:35:51
|
welcome |
 |
|
|
|