Skip to main content
Announcements
Live today at 11 AM ET. Get your questions about Qlik Connect answered, or just listen in. SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

How to use List more efficiently in For Each...Next?

Hello,

I need to use For Each loop to run query on different schema in the same database. All the schema have the same tables with different data. Below is a sample query:

For Each vStore in 's10', 's11', 's12', 's13','s14', 's15', 's16','s17','s18', 's19'

accounts:

LOAD *;

SQL

SELECT companyid, firstname, lastname, custid

FROM $(vStore).customer as a

WHERE date(postdate) = '2011-12-31';

NEXT vStore

In the Code above it just shows 10 schema in the list (after 'in') however, I have 40 schema to run the query on. The editor forces me to add schema in the list in the same line. Is it possible to break the line in the list? when I type the stores in the next line it shows syntax error. Keeping list of 40 schema in the same line makes it difficult to read and edit the script. Any Ideas?

1 Solution

Accepted Solutions
Clever_Anjos
Employee
Employee

Stores:

LOAD * INLINE [

    F1

    s1

    s2

    s3

...

    s50

];

for i = 0 to NoOfRows('Stores') - 1

  let vStore=Peek('F1',i,'Stores');

  accounts:

  LOAD *;

  SQL

  SELECT companyid, firstname, lastname, custid

  FROM $(vStore).customer as a

  WHERE date(postdate) = '2011-12-31';

NEXT vStore

View solution in original post

10 Replies
Colin-Albert

The load concat function can create a table with a single row containing a csv list of store ids Peek this data into a variable, then use this variable as the stores in your for..each loop.

iktrayanov
Creator III
Creator III

Try this

For schema=1 to 50

SQL

SELECT companyid, firstname, lastname, custid

FROM 's$(schema).customer' as a

WHERE date(postdate) = '2011-12-31';

AutoGenerate(1);

next;

maxgro
MVP
MVP

Hi

you can try this

For Each v in 's1', 's2', 's3', 's4'

     For Each vStore in '$(v)0', '$(v)1', '$(v)2', '$(v)3',    //  ..........  '$(v)9'

          trace $(vStore);

          // ......

     NEXT vStore

NEXT v

Clever_Anjos
Employee
Employee

Stores:

LOAD * INLINE [

    F1

    s1

    s2

    s3

...

    s50

];

for i = 0 to NoOfRows('Stores') - 1

  let vStore=Peek('F1',i,'Stores');

  accounts:

  LOAD *;

  SQL

  SELECT companyid, firstname, lastname, custid

  FROM $(vStore).customer as a

  WHERE date(postdate) = '2011-12-31';

NEXT vStore

stevelord
Specialist
Specialist

Hi, do you have a version of this for the variable being in the Where part of the statement instead of the From part of the statement?

I have clients in a client account names field of a file, and I need a script to load itself for one client at a time, then the next, and the next sequentially rather than try to load them all from the file simultaneously.

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

The $() variables will get substituted anywhere in the statement. So if you load up a table of ClientId, and peek() them into a variable named "vClient", your WHERE clause can look like this.

WHERE ClientId='$(vClient)'

-Rob

stevelord
Specialist
Specialist

Thanks, can you assume I’m an idiot and need help on the rest of that syntax? ☺

Say I have these ClientIds: BiffCo, MartyCo, and DocCo

And I have this script:

Load UserId,

ClientId,

Sum(SomeRecords) as PivotedRecords

From C:\Documents\Biometrics.qvd (qvd)

Where ClientId=’$(vClient)’

Group by UserId, ClientId

Can you put the table, peek, and the for each/next stuff precisely where they’d go around that script? I’ve been banging my head on it most of the day and pretty sure I’ve just got some line in the wrong order or the wrong symbol or missing symbol somewhere or other. Rather than share my whole mess, hoping to see a simple clean piece of script that would work independent of anything else. ☺

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

Here's a specific example.

// Load a list of ClientIds from somewhere

ClientTable:

LOAD * INLINE [

aClient

BiffCo

MartyCo

DocCo

];

FOR i = 0 to NoOfRows('ClientTable')-1

LET vClient = peek('aClient',$(i), 'ClientTable');

Load UserId,

          ClientId,

          Sum(SomeRecords) as PivotedRecords

From C:\Documents\Biometrics.qvd (qvd)

Where ClientId=’$(vClient)’

Group by UserId, ClientId

;

NEXT i

Before we get too far afield, I'm wondering why you need to load one client at a time? I just want to understand if we're discussing the best solution.

-Rob

Anonymous
Not applicable
Author

When I got it right:

instead of:

LET vClient = peek('ClientTable', $(i), 'aClient');


it should be:

LET vClient = peek('aClient', $(i), 'ClientTable');


Then it works well. Thanks.