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

Qlikview - Insert a recno() for each change in a column

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

例如:

我的软件:

第三方软件的导入(由于不管理stock/batch受控库存,只采用有限字段):

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

如果我理解正确的话,你要做的是从[Row. [Doc.] 中的每个新值都为 1。编号],并增加字段[行。编号]只要[Doc。 No.]与上一行相同。实现这一目标的一种方法如下:

//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; 

如果有人需要在 SQL-SIDE 中解决此问题:

只需添加到您的 sql 声明中

SELECT ROW_NUMBER() OVER(PARTITION BY [Doc No.] ORDER BY {Anything you want/need} ASC) AS RowNumber,....,....

详细SQL说明在下面link。 (来自 Microsoft 站点,我可以分享我不知道的内容吗,如果没有,抱歉管理员) Microsoft docs link