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 |
Weekend
Starting Member
20 Posts |
Posted - 2006-04-24 : 02:00:48
|
Hello This is my 1st ever post on this forum.Warm wishes for Members & Moderators. Wish to exchange strong long term love, support & learning from all of you. Tab1Col_Item varchar(20),Col_Shop varchar(20),Col_Rate realMany shops sales several items at different rate.I wish to have 3 most min rate shop names with rates against every item.Current StatuesCol_Item || Col_Shop || Col_RateItem1 || Shop1 || 1.2Item1 || Shop2 || 2.0Item1 || Shop3 || 3.0Item1 || Shop4 || 3.2Item2 || Shop1 || 3.0Item2 || Shop2 || 3.3Item2 || Shop3 || 2.5Item2 || Shop4 || 2.9Required Outcome (In 7 Columns)Col_Item || MinShop1 || MinRate1 || MinShop2 || MinRate2 || MinShop3 || MinRate3Item1 || Shop1 || 1.2 || Shop2 || 2.0 || Shop3 || 3.0Item2 || Shop3 || 2.5 || Shop4 || 2.9 || Shop1 || 3.0Please advise how to get the required outcome.WishesJawad |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-04-24 : 02:11:01
|
I guess you need to use Pivot tables, Check out the link if its helps.. http://www.sqlteam.com/item.asp?ItemID=2955If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-24 : 02:11:14
|
[code]declare @table table( Col_Item varchar(20), Col_Shop varchar(20), Col_Rate real)insert into @tableselect 'Item1', 'Shop1', 1.2 union allselect 'Item1', 'Shop2', 2.0 union allselect 'Item1', 'Shop3', 3.0 union allselect 'Item1', 'Shop4', 3.2 union allselect 'Item2', 'Shop1', 3.0 union allselect 'Item2', 'Shop2', 3.3 union allselect 'Item2', 'Shop3', 2.5 union allselect 'Item2', 'Shop4', 2.9select Col_Item, max(case when rowid = 1 then Col_Shop else NULL end) as MinShop1, max(case when rowid = 1 then Col_Rate else NULL end) as MinRate1, max(case when rowid = 2 then Col_Shop else NULL end) as MinRate2, max(case when rowid = 2 then Col_Rate else NULL end) as MinRate2, max(case when rowid = 3 then Col_Shop else NULL end) as MinRate3, max(case when rowid = 3 then Col_Rate else NULL end) as MinRate3from( select Col_Item, Col_Shop, Col_Rate, rowid = (select count(*) from @table x where x.Col_Item = t.Col_Item and x.Col_Rate <= t.Col_Rate) from @table t where Col_Rate in (select top 3 Col_Rate from @table x where x.Col_Item = t.Col_Item order by Col_Rate))agroup by Col_Item[/code] KH |
 |
|
Weekend
Starting Member
20 Posts |
Posted - 2006-04-24 : 02:38:53
|
thanks madhivanan & chiragkhabariaSpecial Thanks for khtan you make it look so simpleWishesJawad |
 |
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-04-24 : 06:42:19
|
Hi all,I noticed that the current solution didn't seem to cope with ties (I don't know whether this was a requirement or not), so I've had a go at improving it.It seems performs a bit quicker too...  --datadeclare @table table( Col_Item varchar(20), Col_Shop varchar(20), Col_Rate real)insert into @tableselect 'Item1', 'Shop1', 1.2 union allselect 'Item1', 'Shop2', 2.0 union allselect 'Item1', 'Shop3', 3.0 union allselect 'Item1', 'Shop4', 3.2 union allselect 'Item2', 'Shop1', 3.0 union allselect 'Item2', 'Shop2', 3.3 union allselect 'Item2', 'Shop3', 2.5 union allselect 'Item2', 'Shop4', 2.9 union allselect 'Item3', 'Shop1', 1.0 union allselect 'Item3', 'Shop2', 2.0 union allselect 'Item3', 'Shop3', 3.0 union allselect 'Item3', 'Shop4', 3.0 union allselect 'Item3', 'Shop5', 5.0 union allselect 'Item4', 'Shop1', 2.0 union allselect 'Item4', 'Shop2', 1.0 union allselect 'Item4', 'Shop3', 1.0 union allselect 'Item4', 'Shop4', 1.0 union allselect 'Item4', 'Shop5', 1.0 --calculationselect Col_Item, max(case when rowid = 1 then Col_Shop else NULL end) as MinShop1, max(case when rowid = 1 then Col_Rate else NULL end) as MinRate1, max(case when rowid = 2 then Col_Shop else NULL end) as MinRate2, max(case when rowid = 2 then Col_Rate else NULL end) as MinRate2, max(case when rowid = 3 then Col_Shop else NULL end) as MinRate3, max(case when rowid = 3 then Col_Rate else NULL end) as MinRate3from ( select a.Col_Item, a.Col_Shop, min(a.Col_Rate) as Col_Rate, count(*) as rowid from @table a inner join @table b on a.Col_Item = b.Col_Item and ((a.Col_Rate != b.Col_Rate and a.Col_Rate >= b.Col_Rate) or (a.Col_Rate = b.Col_Rate and a.Col_Shop >= b.Col_Shop)) group by a.Col_Item, a.Col_Shop having count(*) <= 3 ) agroup by Col_Item Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
Weekend
Starting Member
20 Posts |
Posted - 2006-04-24 : 06:58:27
|
thanks Ryan RanballI was facing the same problem.WishesJawad |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-04-24 : 07:19:50
|
Jawad -- are you using sql 2005 by any chance? |
 |
|
Weekend
Starting Member
20 Posts |
Posted - 2006-04-24 : 10:16:55
|
No it SQL Server 2000 |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-04-24 : 10:45:52
|
Too bad, this is *really* easy to do in SQL 2005, and much more efficient as well. |
 |
|
|
|
|
|
|