Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 view
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Joetaniarto00
Starting Member

Indonesia
12 Posts

Posted - 10/20/2014 :  03:11:13  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

2780 Posts

Posted - 10/20/2014 :  09:18:12  Show Profile  Reply with Quote
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

Indonesia
12 Posts

Posted - 10/20/2014 :  23:33:03  Show Profile  Reply with Quote
could I do it without creating table?
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000