Allright, solution to "Tough select/join" is now well placed within the procedures they need to, but now I have a slight abbrevation that I can't figure out (and I feel totally stupid because I belive it should be quite easy given the solution I allready have). There is an additional table "Single" that I need to join in a certain way.Single:--------------------------------------------------------------SingleID | UserID | DescID | PlayerID | Price | Volume |-------------------------------------------------------------- 1 | 1 | 15 | 256 | 2 | 150 | 2 | 2 | 27 | 141 | 3.2 | 650 | 3 | 1 | 17 | 322 | 3 | 500 | 4 | 2 | 36 | 832 | 1.7 | 120 |--------------------------------------------------------------Multi: MultiDetail:--------------------------------------- ----------------------------------|MultiID | UserID | Price | Volume | | MultiID | DescID | PlayerID |--------------------------------------- ---------------------------------- 1 | 11 | 5.1 | 300 | | 1 | 15 | 256 | 2 | 12 | 2 | 500 | | 1 | 17 | 322 | 3 | 13 | 4.7 | 900 | | 2 | 36 | 956 | 4 | 14 | 2.1 | 650 | | 2 | 58 | 156 |--------------------------------------- | 2 | 58 | 156 | | 3 | 15 | 256 | | 3 | 17 | 322 | | 4 | 36 | 956 | | 4 | 58 | 156 | ----------------------------------
As you may notice after looking a bit, UserID 1 in Single has 2 rows that together match the same combination of DescID/PlayerID as MultiID # 1 in MultiDetail. What I need to find is all MultiIDs, single-prices multiplied together and single-volumes added together where a Single.UserID can match *all* of the DescID/PlayerID-pairs in MultiDetail. Single.UserID is given, together with *one* pair of Desc/PlayerID. To explain better I have created this pseudo-code:SELECT MultiID, EXP(SUM(LOG(Single.Price))), SUM(Single.Volume)FROM Single join Multi join MultiDetailWHERE Single.DescID = 15 AND Single.PlayerID = 156AND Single.UserID = 1AND MultiDetail.DescID = Single.DescIDAND MultiDetail.PlayerID = Single.PlayerIDAND EXP(LOG(SUM(Single.Price))) >= Multi.PriceDDL:DECLARE @UserID int, @DescID int, @PlayerID int --> This is input, relative to the first row in SingleSET @UserID = 1SET @DescID = 15SET @PlayerID = 256DECLARE @Single table (SingleID int, UserID int, DescID int, PlayerID int, Price decimal(4, 2), Volume int)DECLARE @Multi table (MultiID int, UserID int, Price int, Volume int)DECLARE @MultiDetail table (MultiID int, DescID int, PlayerID int)INSERT INTO @Single SELECT 1, 1, 15, 256, 2, 150 UNION ALL SELECT 2, 2, 27, 141, 3.2, 650 UNION ALLSELECT 3, 1, 17, 322, 3, 500 UNION ALL SELECT 4, 2, 36, 832, 1.7, 120INSERT INTO @MultiSELECT 1, 11, 5.1, 300 UNION ALL SELECT 2, 12, 2, 500 UNION ALL SELECT 3, 13, 4.7, 900 UNION ALL SELECT 4, 14, 2.1, 650 INSERT INTO @MultiDetailSELECT 1, 15, 256 UNION ALL SELECT 1, 17, 322 UNION ALLSELECT 2, 36, 956 UNION ALL SELECT 2, 58, 156 UNION ALLSELECT 2, 15, 256 UNION ALL SELECT 3, 15, 256 UNION ALLSELECT 3, 17, 322 UNION ALL SELECT 4, 36, 956 UNION ALLSELECT 4, 58, 156SELECT * FROM @SingleSELECT * FROM @MultiSELECT * FROM @MultiDetail
--Lumbago"Real programmers don't document, if it was hard to write it should be hard to understand"