?
Solved

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

Posted on 2014-02-28
3
Medium Priority
?
1,618 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

752 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