【问题标题】:How to achieve concatenate the records from flat file?如何实现连接平面文件中的记录?
【发布时间】:2019-10-03 23:23:19
【问题描述】:

我在文本文件中有两个代码(001 和 002)如果行以 002 代码开头,则行数量应与下一个 001 代码行数量相加。应为每个 001 代码行重复该序列。表示每个 001 代码行的数量应该是与前一个 002 行数量的总和(即 b/w 每个 001 代码行的数量应该与下一个 001 行数量连接)

输入文件中的实际行

001 | 0.00

002 | 10.5

002 | 5.0

001 | 0.00

002 | 15.0

001 | 5

002 | 7

001 | 2

输入文件的预期输出行

001 | 0.00

002 | 10.5

002 | 5.0

001 | 15.5 ( adding amount with 2nd, 3rd-row amount)

002 | 15.0

001 | 20.0 (adding amount with previous 002 code amount)

002 | 7.0

001 | 9.0  (adding amount with previous 002 code amount)

【问题讨论】:

标签: c# ssis flat-file


【解决方案1】:

脚本组件(转换): 将 Col1 标记为只读 将 Col2 标记为读/写

这里的诀窍是您需要在行处理之外存储信息,因此您需要在行处理之外声明变量。

行外处理

//these are established on the first pass and used on every row moving forward.
decimal first002;
decimal last002;
int ctr=0;

行内处理:

if(Row.Col1=="001")
{
    //Do something with COl2 based on ctr
    switch(ctr)
    { 
        case 0:
             break; //don't do anything
        case 1:
             Row.Col2 = first002;
             break;
        case 2:
             Row.Col2 = first002+last002;
             break;
        default:
             Row.Col2 += last002;
             break;
    }
}
else
{
   //this is a 002 and need to update variables.
   ctr++
   if(ctr==1)
   {first002 = Row.Col2;}
   else
   {last002 = Row.Col2;}
}

【讨论】:

  • 为什么这被否决了?当跨行进行算术运算时,可能需要一个脚本组件来将前一行的值写入变量,以便可以使用当前行处理下一行的值。尝试在查询中执行此操作将具有挑战性,尽管 LEAD/LAG 分区函数可能会有所帮助。
  • 好吧,他使用整数,而输入是小数。不处理空值并增加了更多复杂性,并且比使用基于 SQL 的解决方案更慢。
  • 原始问题使用整数作为样本数据。可以轻松修改。
猜你喜欢
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
相关资源
最近更新 更多