Solved

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

Posted on 2014-02-28
3
1,485 Views
Last Modified: 2016-02-10
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
Comment
Question by:canuckconsulting
3 Comments
 
LVL 35

Expert Comment

by:David Todd
ID: 39896875
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
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39896969
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
 

Author Closing Comment

by:canuckconsulting
ID: 39913863
Perfect...thanks!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
My client has a dictionary table. They're defining a list of standard naming convention. Now, they are requiring my team to provide us a mechanism how to match new incoming data with existing data in their system.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question