• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1958
  • Last Modified:

SSiS How to transform multiple rows into different columns of one row

I have one DB with a table defined as follows:

ProductsInStock1
ProductID
Size
Qty

If we had product 1 available in small medium and large the three records would look like:

1 | Small | 92
1 | Medium | 33
1 | Large | 30

I need to transform this using SSIS to a table defined as:

ProductsInStock2
ProductID
QtySmall
QtyMedium
QtyLarge
QtyExtraLarge

For the same product above I would want a single row as follows:

1 | 92 | 33 | 30 | NULL

How can I transform the data as required?
0
canuckconsulting
Asked:
canuckconsulting
1 Solution
 
David ToddSenior DBACommented:
Hi,

Wouldn't this be best done as a pivot query?

use ExpertsExchange
go

if exists(
	select *
	from sys.objects
	where
		object_id = object_id( N'dbo.ProductsInStock' )
		and type in( N'U' )
	)
	drop table dbo.ProductsInStock
;
go

if not exists(
	select *
	from sys.objects
	where
		object_id = object_id( N'dbo.ProductsInStock' )
		and type in( N'U' )
	)
	create table dbo.ProductsInStock (
		ProductID int
		, size nvarchar( 20 )
		, Qty int
	) on [PRIMARY]
go

insert dbo.ProductsInStock 
	values( 1, 'Small', 92 )
	, ( 1, 'Medium', 33 )
	, ( 1, 'Large', 30 )
;

select *
from dbo.ProductsInStock
;

select 
	p.ProductID
	, Small
	, Medium
	, Large
	, ExtraLarge
from dbo.ProductsInStock s
pivot (
	sum( s.Qty )
	for s.size in ( [Small], [Medium], [Large], [ExtraLarge] )
	) p
order by
	p.ProductID
;

Open in new window


HTH
  David
0
 
Surendra NathTechnology LeadCommented:
what todd said above definitely works, but in order to get that working you have to use the SQL task and pass the input to the sql server database.

But if you dont want to send the data to the sql server and get it back or if you want to use the pure SSIS version, there is an inbuilt Pivot transformation in the SSIS which you can use.

Inorder to do that, use the pivot transformation in the data flow task by setting the below parameters

size is selected in the Pivot Key list box.
ProductID is selected in the Set Key list box.
quantity is selected in the Pivot Value list box.
The following values are entered in the Generate pivot output columns from values box.
[Small], [Medium], [Large], [ExtraLarge]

you can also follow the below link for more information.
http://technet.microsoft.com/en-us/library/ms140308.aspx
0
 
canuckconsultingAuthor Commented:
Perfect...thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now