Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
datanibbler
Champion
Champion

Loop - check whether one record has several inside


Hi,

in the context of a larger subroutine, I have a small piece of code to

- parse through the contents of a list one record at a time

- check whether one of the records has several records inside (look at the attached sample and you'll see)

- if so, join a fixed '1' to the table being parsed, using the iteration_nr (which is identical to the row_nr. in the table)

- if not, join a '0' to the table being parsed

=> That binary field (1 or 0) will be used in the next step to split the table in two.

In the attached sample, three records are in order, so they should get a 0 joined to them as per the piece of code I post here;

<=> One record has two inside, so it should get a 1 at this point.

The code of the subroutine (being called once per record, four times in this instance) looks like this (the counter_variable is v_line)

SOFA_pre2

LOAD
    
Mat as Mat_pre
 
RESIDENT SOFA_Liste_v1
 
WHERE Line_SOFA_pre = $(v_line);
 
NoConcatenate
  SOFA_post:
 
LOAD
      *
 
From_Field (SOFA_pre2, Mat_pre)
  (
no labels);
 
DROP TABLE SOFA_pre2;
 
IF NoOfRows('SOFA_post') > 1 THEN
  
LEFT JOIN (SOFA_Liste_v1)
  
FIRST 1 LOAD $(v_line) as Line_SOFA_pre, 1 as Flag_Multi RESIDENT SOFA_post;
 
ELSE
  
LEFT JOIN (SOFA_Liste_v1)
  
FIRST 1 LOAD $(v_line) as Line_SOFA_pre, 0 as Flag_Multi RESIDENT SOFA_post;
 
END IF
 
DROP TABLE SOFA_post;

Right now, that does not work - of those four records, only the first one (which is good as per that logic) gets a 0 joined to it. This piece of code runs fine and looking at the Debugger, I see it goes through the lines and it correctly identifies the last record as a "multi_record" and it goes into the IF_THEN_block - otherwise, it always jumps to the ELSE_block - but for some reason, the JOIN doesn't work - and in the next step, only one record is declared "clean" because it has the 0.

Can anybody help me there?`

Thanks a lot!

Best regards,

DataNibbler

10 Replies
datanibbler
Champion
Champion
Author

Hi flipside,

I have another issue now:

=> At one point in the code (just where I entered that check yesterday) I have a check whether the table I loaded is empty => if it is, I actually don't need to process it further and I can leave, so I have an IF_THEN_block and an EXIT SUB within.

<=> The issue is, when I do that, the Subroutine is ended and no qvd_file is saved - well, there's not much of a point if there is nothing, but I have to build something to delete qvd_files - if there are any - after the entire code has run so I won't have issues with old qvd_files the next time round - but in case no qvd_file is created, I cannot delete it...