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
 Other SQL Server Topics (2005)
 view

Author  Topic 

Joetaniarto00
Starting Member

12 Posts

Posted - 2014-10-20 : 03:11:13
Hi,

I Have data :
ID Item qty
001 Alba 100
001 Alba 200
001 Alba 80
002 Seiko 100
002 Seiko 250

I want to make change the view into :
ID item qty1 qty2 qty3
001 Alba 100 200 80
002 Seiko 100 250 0

Please tell me how...

thanks,


gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-20 : 09:18:12
This should get you going:


declare @t as table (a int, b varchar(10), c int)
insert into @t (a,b,c) values

(001,'Alba',100 ),
(001,'Alba',200 ),
(001,'Alba',80 ),
(002,'Seiko',100 ),
(002,'Seiko',250 )
;

with src as (
select a, b, c,
rn=row_number() over(partition by a, b order by (select null))
from @t
)

select a, b, isnull([1],0) as qty1, isnull([2],0) as qyt2, isnull([3],0) as qty3
from src
pivot (max(c) for rn in ([1],[2],[3])) pvt



Go to Top of Page

Joetaniarto00
Starting Member

12 Posts

Posted - 2014-10-20 : 23:33:03
could I do it without creating table?
Go to Top of Page
   

- Advertisement -