SSIS - 计算期初和期末余额

SSIS - Calculate Opening and Closing Balance

我需要在 SSIS 中计算期初余额和期末余额。我有以下数据作为输入。

invoice_date    amount
12/4/2016       4000
12/5/2016       5000
12/6/2016       7500
12/7/2016       5000
12/8/2016       8000

我想要如下输出:

Opening Balance    4000
Closing Balance    8000

如何在 SSIS 中实现此目的?

注意:只需要使用转换即可。无需执行 SQL 任务或 OLEDEB 命令。

在我的回答中,我假设您的来源是 OLEDB Source,您的目的地是 Flat File

您必须执行以下步骤:

  1. 再添加一个Dataflow Task (假设nema = DFT Import
  2. DFT Import 添加你的 OLEDB Source ,一个 Script Component 和你的 FlatFile Destination
  3. 在脚本组件中,将 invoice_dateamount 列标记为输入列

  1. 在脚本中转到 Inputs and Outputs 选项卡并使您的 Output Buffer 异步

  1. 创建 2 个输出列 *(Desc 类型 DT_STRamount 类型 DT_I4

  1. 在您的脚本中写入以下代码:(Vb.net)

    Dim MinDate, MaxDate As Date
    Dim MinAmount, MaxAmount As Integer
    Dim intRowCount As Integer = 0
    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
    
        intRowCount += 1
    
        If intRowCount = 1 Then
    
            MinDate = Row.invoicedate
            MaxDate = Row.invoicedate
            MinAmount = Row.amount
            MaxAmount = Row.amount
    
        Else
    
            If Row.invoicedate < MinDate Then
    
                MinDate = Row.invoicedate
                MinAmount = Row.amount
    
            ElseIf Row.invoicedate > MaxDate Then
    
                MaxDate = Row.invoicedate
                MaxAmount = Row.amount
    
            End If
    
        End If
    
    End Sub
    
    Public Overrides Sub PostExecute()
        MyBase.PostExecute()
    
        Output0Buffer.AddRow()
        Output0Buffer.Desc = "Opening Balance"
        Output0Buffer.amount = MinAmount
    
    
        Output0Buffer.AddRow()
        Output0Buffer.Desc = "Closing Balance"
        Output0Buffer.amount = MaxAmount
    
    End Sub
    
  2. 将输出列映射到目标列

注意:如果您的源列数据类型不是 datetimeinteger,您必须在脚本中执行一些转换方法

其他方法

  1. 添加一个Execute SQL Task以获得源的行数Table
  2. 将计数值 (结果集) 存储到 SSIS 变量 (例如:User::intCount

您可以使用包含 OLEDB SourceRowcount 组件的数据流任务来代替前两个步骤,并将行计数结果存储到变量中

  1. 按照第一种方法的相同步骤
  2. 在脚本中添加 User::intCount 作为只读变量
  3. 在脚本中写入如下代码

    Dim MinDate, MaxDate As Date
    Dim MinAmount, MaxAmount As Integer
    Dim intRowCount As Integer = 0
    Dim intCurrentRow As Integer = 0
    
    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
    
        intCurrentRow += 1
    
        If intCurrentRow = 1 Then
    
            MinDate = Row.invoicedate
            MaxDate = Row.invoicedate
            MinAmount = Row.amount
            MaxAmount = Row.amount
    
        Else
    
            If Row.invoicedate < MinDate Then
    
                MinDate = Row.invoicedate
                MinAmount = Row.amount
    
            ElseIf Row.invoicedate > MaxDate Then
    
                MaxDate = Row.invoicedate
                MaxAmount = Row.amount
    
            End If
    
            If intCurrentRow = intRowCount
    
    
            Output0Buffer.AddRow()
            Output0Buffer.Desc = "Opening Balance"
            Output0Buffer.amount = MinAmount
    
            Output0Buffer.AddRow()
            Output0Buffer.Desc = "Closing Balance"
            Output0Buffer.amount = MaxAmount
    
           End If
    
        End If
    
    End Sub
    
    Public Overrides Sub PreExecute()
        MyBase.PreExecute()
    
        IntRowCount = Variables.intCount
    
    End Sub