Qlikview-为列中的每个更改插入一个recno()

时间:2018-11-13 10:30:21

标签: sql qlikview

我有一张带有文件编号和项目的表格。我们已将软件集成到第三方会计软件包中。导入第三方不会使用我们的文档行号,但是会以相同的顺序发布到他们的后端。

例如:

我的软件:

enter image description here

导入第三方软件(仅用于有限的字段,因为它们不管理库存/批次控制的库存):

enter image description here

我的目标是在第二个表中创建一个新列,以为文档编号中的每个更改添加行号。这将允许我创建一个唯一的键,这是将两个数据库的表链接在一起所需的键。

enter image description here

1 个答案:

答案 0 :(得分:0)

If I understand the issue correctly, what you want to do is to start with [Row. No.] 1 for every new value in [Doc. No.], and increment the field [Row. No.] by 1 as long as [Doc. No.] is the same as the previous row. One way to accomplish that could be the following:

//load the table, in this example it is an inline table 
//but you would load it from another source
    table2:
    load * inline [
    Doc. No.,Description,Qty,Batch,Value
    Doc 1, Item1, 1, 10
    Doc 1, Item1, 2, 10
    Doc 1, Item1, 3, 10
    Doc 2, Item2, 1, 20
    Doc 3, Item3, 1, 30
    Doc 3, Item3, 1, 30
    Doc 3, Item3, 1, 30
    ];

//define an empty table that into which values can be "concatenateloaded"
    newTable2:
    load * inline [
    dummy
    1
    ];


//define starting values to be used in the loop
    let rownodigit = 0;
    let lastdocno = '';


//Iterate over each row in the table, and use the values to build 
//the new table row for row. If the table is large this would 
//require a significant amount of time...

FOR rowno = 0 TO noOfRows('table2')-1
    let docno = peek('Doc. No.', rowno, 'table2');
    let desc = peek('Description', rowno, 'table2');
    let qty = peek('Qty', rowno, 'table2');
    let batch = peek('Batch', rowno, 'table2');
    let value = peek('Value', rowno, 'table2');

    //determine what value the [Row. No.] field is to be given
    if docno=lastdocno then
        rownodigit = rownodigit + 1;
    else
        rownodigit = 1
    endif

    //build the table by generating a new row into the new table
    concatenate (newTable2) 
    load 
        '$(docno)' as [Doc. No.],
        '$(desc)' as [Description],
        $(qty) as [Qty],
        $(batch) as [Batch],
        $(value) as [Value],
        $(rownodigit) as [Row. No.]
    autogenerate (1)
    ;

    let lastdocno = docno; //store the value of docno into a new variable
                           //for comparison in the top of the loop in 
                           //the next iteration
NEXT rowno

drop field dummy; //this field was only needed to create the temporary table
drop table table2; //drop the orgiginal table, otherwise we would have 
                   //a lot of synthetic keys

//now fix the table and create the [Key] field
    table2:
    load *, [Doc. No.]&[Row. No.]&[Description] as Key
    resident newTable2
    where len([Doc. No.])>0 //this avoids the blank row generated 
                            //by the first dummy insert
    ;

//this was a temporary table that we generated and it should now be dropped
drop table newTable2;