【问题标题】:SSIS - Calculate Opening and Closing BalanceSSIS - 计算期初和期末余额
【发布时间】:2017-03-22 07:38:35
【问题描述】:

我需要计算 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 命令。

【问题讨论】:

    标签: sql-server ssis etl ssis-2012 bids


    【解决方案1】:

    在我的回答中,我会假设您的 Source 是 OLEDB Source,而您的 Destination 是 Flat File

    您必须执行以下步骤:

    1. 添加另一个 Dataflow Task (假设 nema = DFT Import
    2. DFT Import 中添加您的OLEDB SourceScript 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获取源表的行数
    2. 将计数值 (Resultset) 存储到 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
      

    【讨论】:

    • 我按照你的步骤.. 但我没有得到任何输出
    • @YousufSultan 遇到任何异常?还是脚本组件没有显示任何输出?
    • @YousufSultan 我添加了另一个方法,看看
    • @YousufSultan 等待你的回复??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    相关资源
    最近更新 更多