Author |
Topic |
1wrexham1
Starting Member
2 Posts |
Posted - 2010-07-17 : 10:33:27
|
Hi Experts!I have 3 tablestblPricePriceID|SupplierID|SkipID|PostcodeID|Cost1|1|1|1|1002|1|2|1|2003|1|3|1|3004|1|4|1|4005|1|5|1|5006|2|1|1|507|2|2|1|2508|2|3|1|2509|2|4|1|45010|2|5|1|550tblSkipSkipID|Name|Description|Image1|Mini|aaa|aaa2|Midi|bbb|bbb3|Large|ccc|ccc4|XLarge|ddd|ddd5|XXLarge|eee|eeetblMarginMarginID|SkipID|Margin1|1|102|2|203|3|304|4|405|5|50I need to get table like this:SupplierID|SkipID|MinCost|Name|Description|Image|Margin2|1|50|Mini|aaa|aaa|101|2|200|Midi|bbb|bbb|202|3|250|Large|ccc|ccc|301|4|400|XLarge|ddd|ddd|401|5|500|XXLarge|eee|eee|50I need to get from my first 3 tables cheapest price for each skipID where PostcodeID = @PostcodeID and get rest of the values as it is shown in fourth table... PLEASE HELP |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-17 : 10:45:18
|
[code]DECLARE @tblPrice TABLE( PriceID int, SupplierID int, SkipID int, PostcodeID int, Cost int)INSERT INTO @tblPriceSELECT 1,1,1,1,100 UNION ALLSELECT 2,1,2,1,200 UNION ALLSELECT 3,1,3,1,300 UNION ALLSELECT 4,1,4,1,400 UNION ALLSELECT 5,1,5,1,500 UNION ALLSELECT 6,2,1,1,50 UNION ALLSELECT 7,2,2,1,250 UNION ALLSELECT 8,2,3,1,250 UNION ALLSELECT 9,2,4,1,450 UNION ALLSELECT 10,2,5,1,550DECLARE @tblSkip TABLE( SkipID int, [Name] varchar(10), [Description] varchar(10), [image] varchar(10))INSERT INTO @tblSkipSELECT 1,'Mini','aaa','aaa' UNION ALLSELECT 2,'Midi','bbb','bbb' UNION ALLSELECT 3,'Large','ccc','ccc' UNION ALLSELECT 4,'XLarge','ddd','ddd' UNION ALLSELECT 5,'XXLarge','eee','eee'DECLARE @tblMargin TABLE( MarginID int, SkipID int, Margin int)INSERT INTO @tblMarginSELECT 1,1,10 UNION ALLSELECT 2,2,20 UNION ALLSELECT 3,3,30 UNION ALLSELECT 4,4,40 UNION ALLSELECT 5,5,50SELECT p.SupplierID, p.SkipID, p.Cost, s.[Name], s.[Description], s.[image], m.MarginFROM ( SELECT *, row_no = row_number() OVER (PARTITION BY p.SkipID ORDER BY Cost) FROM @tblPrice p ) p INNER JOIN @tblSkip s ON p.SkipID = s.SkipID INNER JOIN @tblMargin m ON p.SkipID = m.SkipIDWHERE p.row_no = 1[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
1wrexham1
Starting Member
2 Posts |
Posted - 2010-07-17 : 10:59:08
|
Worked perfect!! Thanks a lot... Because I have lots of different PostcodeID in database, where should I insert WHERE PostcodeID = @PostcodeID filter?? |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-07-17 : 11:40:24
|
[code]WHERE p.row_no = 1AND p.PostcodeID = @PostcodeID[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-07-17 : 11:42:23
|
Insert it hereSELECT *, row_no = row_number() OVER (PARTITION BY p.SkipID ORDER BY Cost) FROM @tblPrice p where PostcodeID = @PostcodeID Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
|
|
|