Solved

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

Posted on 2014-02-28
3
1,433 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

757 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now