Qlik Community

QlikView Creating Analytics

Discussion Board for collaboration related to Creating Analytics for QlikView.

Not applicable

Looping through Table Rows

I want to Loop through each row in a table, set variables to the values of each row in order to do some repetitive processing.  Seems like a simple construct that would be used often.  Here is what I thought would work, but for some reason the data coming back doesn't seem to match across the column for each row returned, and as I work through index by third iteration I'm getting nulls on the second column.

BOX:

LOAD * INLINE [BOXName, VOLCnt
"MRC", 49152
"TTN", 49152
"PLT", 12288
"GLD", 12288
"SVR", 12288
"IRN", 12288
]
;

LET NumRows=NoOfRows('BOX');
FOR i=1 to $(NumRows)
 
LET vBox=FieldValue('BOXName',$(i));
 
LET vVolCnt=FieldValue('VOLCnt',FieldIndex('BOXName','$(vBox)'));
//do some stuff....
NEXT;

Result set for variables after run:

vBoxvVolCnt
MRC49152
TTN

12288

PLT<NULL>
GLD

<NULL>

SVR<NULL>
IRN<NULL>

Seems I'm missing something very elementary here

1 Solution

Accepted Solutions
danielrozental
Honored Contributor II

Re: Looping through Table Rows

You should be using Peek instead,

  LET vBox=Peek('BOXName',$(i));

  LET vVolCnt=Peek('VOLCnt',$(i));

11 Replies
danielrozental
Honored Contributor II

Re: Looping through Table Rows

You should be using Peek instead,

  LET vBox=Peek('BOXName',$(i));

  LET vVolCnt=Peek('VOLCnt',$(i));

MVP
MVP

Re: Looping through Table Rows

.. and specify the table name in your peak function, as a third parameter

Not applicable

Re: Looping through Table Rows

Daniel,

Thanks for the quick reply.  It seems I totally overthought this one

Peek with the index works!

Cheers,

Lew

Not applicable

Re: Looping through Table Rows

Oleg,

It works perfect without a third parameter of table name, but for grins I tried adding this third parameter...it didn't break anything, but the result was the creation of a new variable of the same name as table that would get set to NULL on each peek.

Daniel's suggestion worked as is, although seem tables have origin 0 not 1 in QV, so had to change my FOR:

LET

NumRows=NoOfRows('BOX');
FOR i=0 to $(NumRows)-1
 
LET vBox=Peek('BOXName',$(i));
 
LET vVolCnt=peek('VOLCnt',$(i));
  //do some repetitive stuff....
NEXT;

MVP
MVP

Re: Looping through Table Rows

I might be over thinking it, but it might be important when you have the same value repeating multiple times in different tables rows, and possibly the same field present in multiple tables. then, when you specify

peek ('field', index)

it becomes unclear what the index should represent - the value index in a field list, or a row in a table? ...what table?

the two formats might work identically in simple cases when the field only exists in the same table, and when values are fairly unique.

Not applicable

Re: Looping through Table Rows

Well Oleg, thought you might be onto something here. Once I put an SQL Select into the loop, the "Peeks" returned NULL on the second iteration as though somehow the SQL statement cleared the values from the Inline Table.  Started thinking your qualification with the third parameter might be the ticket, but didn't make a difference

Any thoughts on why peek is acting differently now that there is an SQL statment in the mix?

LET NumRows=NoOfRows('BOX');
FOR i=0 to $(NumRows)-1
LET vBox=Peek('BOXName',$(i));
LET vVolCnt=peek('VOLCnt',$(i));
LET my_include= '..\..\Include\BDS-IT\odbc-' & '$(vBox)' & '.qvs';
$(include=$(my_include));

DISC_USAGE:
SQL SELECT
     '$(vBox)' AS SRC_SYS_CD,
     DISC_DATE,
     VOL_TYPE,
     MIN_FREE_TB,
     MAX_FREE_TB,
      ...
     FROM   NEO.DISK_USAGE_VIEW           

     WHERE
              VOLUME_NAME LIKE '$FC%'
               ...

      HAVING

             VOL_CNT = $(vVolCnt)
       FOR READ UNCOMMITTED ACCESS IN SHARE MODE;
NEXT;

MVP
MVP

Re: Looping through Table Rows

Your SQL statement appears after the peek()... you don't show the part of the script tha loads the previous table...

The SQL statement by itself shouldn't change much. If your previous statement is also a new SQL, - check if your fields have the same upper-lower case as you spelled them in the peek(). It looks like your SQL only returns upper case fields...

Not applicable

Re: Looping through Table Rows

The only previous statement is the INLINE LOAD....so very puzzled   Doesn't seem like the SQL SELECT should have any effect on these "Peeks", but it does.

BOX:

LOAD * INLINE [BOXName, VOLCnt
"MRC", 49152
"TTN", 49152
"PLT", 12288
"GLD", 12288
"SVR", 12288
"IRN", 12288
]
;

LET NumRows=NoOfRows('BOX');
FOR i=0 to $(NumRows)-1
LET vBox=Peek('BOXName',$(i),BOX);
LET vVolCnt=peek('VOLCnt',$(i),BOX);
LET my_include= '..\..\Include\BDS-IT\odbc-' & '$(vBox)' & '.qvs';
$(include=$(my_include));

DISC_USAGE:
SQL SELECT
     '$(vBox)' AS SRC_SYS_CD,
     DISC_DATE,
     VOL_TYPE,
     MIN_FREE_TB,
     MAX_FREE_TB,
      ...
     FROM   NEO.DISK_USAGE_VIEW           

     WHERE
              VOLUME_NAME LIKE '$FC%'
               ...

      HAVING

             VOL_CNT = $(vVolCnt)
       FOR READ UNCOMMITTED ACCESS IN SHARE MODE;
NEXT;

torstein
New Contributor

Re: Looping through Table Rows

Peek is way to slow. How do I loop trough the data in a faster way?

Community Browser