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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 pivot a table

Author  Topic 

itaybarda
Starting Member

12 Posts

Posted - 2007-05-16 : 11:20:05
hi.
this is a very hard sql.
if someone can do it without a cursor and in 1 sql, it will be very good.

this is my table:
item | store | Price
aaa |store1 | 20
aaa |store8 | 19
aaa |store6 | 50
aaa |store55| 64
aaa |store3 | 15
bbb |store1 | 12
bbb |store8 | 20
bbb |store6 | 15
bbb |store55| 16
bbb |store3 | 20

i want the sql to select the 3 lowest prices per item (only the lowes 3), and show each item in a column, like this:

item | Store1 | Price1 | store2 | Price2 | Store3 | Price3
-------------------------------------------------------------
aaa |store3 |15.00 | store8 | 19.00 | store1 | 20.00
bbb |store1 |12.00 | store6 |15.00 |store55 | 16.00

thanks

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-05-16 : 11:29:55
Read about [bCross-tab Reports[/b] in sql server help file

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-05-16 : 11:32:13
Use the new PIVOT operator together with ROW_NUMBER() function.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

itaybarda
Starting Member

12 Posts

Posted - 2007-05-16 : 11:43:57
Peso. can you give me example of how to do it?
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-05-16 : 11:47:00
See this:
[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=79951&SearchTerms=PIVOT,operator[/url]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-05-16 : 11:50:24
[code]-- prepare sample data
declare @ table (item varchar(3), store varchar(7), price int)

insert @
select 'aaa', 'store1', 20 union all
select 'aaa', 'store8', 19 union all
select 'aaa', 'store6', 50 union all
select 'aaa', 'store55', 64 union all
select 'aaa', 'store3', 15 union all
select 'bbb', 'store1', 12 union all
select 'bbb', 'store8', 20 union all
select 'bbb', 'store6', 15 union all
select 'bbb', 'store55', 16 union all
select 'bbb', 'store3', 20

-- show the expected result
select d.item,
max(case when d.recid = 1 then d.store end) as store1,
max(case when d.recid = 1 then d.price end) as store1,
max(case when d.recid = 2 then d.store end) as store2,
max(case when d.recid = 2 then d.price end) as store2,
max(case when d.recid = 3 then d.store end) as store3,
max(case when d.recid = 3 then d.price end) as store3
from (
select item,
store,
price,
row_number() over (partition by item order by price) as recid
from @
) as d
where d.recid between 1 and 3
group by d.item[/code]

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

itaybarda
Starting Member

12 Posts

Posted - 2007-05-16 : 12:02:44
WOW....than you very much peso and harsh_athalye..
it's a perfectly working sql. very proffessional.
Go to Top of Page
   

- Advertisement -