' Microsoft SQL Server Integration Services user script component ' This is your new script component in Microsoft Visual Basic .NET ' ScriptMain is the entrypoint class for script components Imports System Imports System.Data Imports System.Math Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper Imports Microsoft.SqlServer.Dts.Runtime.Wrapper Public Class ScriptMain Inherits UserComponent Dim WeekID As Integer Dim Counter As Integer = 0 Dim PrevWeekID As Integer Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer) WeekID = Row.weekID If WeekID = PrevWeekID Then Row.Rank = Counter + 1 Counter = Counter + 1 Else Row.Rank = 1 Counter = 1 End If PrevWeekID = Row.weekID End Sub End Class
I started keeping notes on things I used a lot, or that took care of a difficult problem. On looking for something I couldn't remember how to do, and spending 30 minutes going through notes and SSIS projects, I decided to create this blog. Lots of SQL, BI, Web, Winforms, Spotifre, R, Python and whatever I happen to be working in when I decide to post something I find interesting.
Wednesday, 5 December 2012
Sequence number (Rank function) in SSIS
This example takes a table from SQL Server, orders the columns and creates a Rank (or sequence number). Works well and I've used it many times.
First drag a new DataFlow into your package:
Next write your SQL statement to gather your rows. Pay attention to the ordering. WK is the field I will be ranking on and WeekID is the field I will be partitioning on.
Now we have to tell SSIS that we have ordered the way we have (this circumvents the need for a Sort component, which is slow and inefficient).
This has to be done for the 'Error Output' too.
Next we tell the package what order the fields are ordered in... In this case WK is sort position 1 and WeekID is position 2.
Next drag a Script Component on to the DataFlow.
Select the fields you want to work with in the code.
Add an new column that will contain the rank numbers.
Next click on 'Design' to add your code.
Here is the code you need to add:
Labels:
Integration Services,
Rank,
Sequence,
SQL,
SSIS
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment