Solved

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

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

863 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

29 Experts available now in Live!

Get 1:1 Help Now