Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Loop all table rows

Hello.

I have a table like this:

materialpriceqty
66929€ 1,203
66929€ 1,255
66929€ 1,702
15894€ 5,1010
66929€ 1,203

For simplicity, let say we have only one material (66929). What I need is work out this table into another one like this:

materialno.price
669291€ 1,20
669292€ 1,20
669293€ 1,20
669294€ 1,25
669295€ 1,25
669296€ 1,25
669297€ 1,25
669298€ 1,25
669299€ 1,70
6692910€ 1,70
6692911€ 1,20
6692912€ 1,20
6692913€ 1,20

As you can see, in this last table I have listed all the materials one by one, according to their quantity. I have therefore three items at € 1,20, five at € 1,25 and so on.

In QlikView, I need two loops: one that scrolls each row and a nested one that will loop as many time as stated in the corresponding quantity. I'll thus create this second table including the RowNo() (called "no." in the example).
I'm able to loop all the rows, but I can't loop the single row according the the reported quantity. Any idea, please?

The code I've written till now is (assuming "Details" is the first table and "r" is the RecNo() of the first table, calculated and included for each row):

LET NrRows = NoOfRows('Details');
LET k = 1; //counter for rows
DO WHILE r<=NrRows //loop all the rows
LET i=0;
DO WHILE i<PEEK(qta_originale,r,'Details'); //I need to loop the selected row as many times as quantity is. What I've writtend here doesn't work since the PEEK function is not in a load statement
LOAD RowNo() as n, PEEK(material,r,'Details') as material, PEEK(price,r,'Details') as price
RESIDENT Details;
i=i+1;
LOOP
k=k+1;
LOOP


1 Solution

Accepted Solutions
johnw
Champion III
Champion III

A completely different loopless approach:

Raw:
LOAD *
,1 as start
,qty as end
,recno() as recno
INLINE [
material,price,qty
66929,1.20,3
66929,1.25,5
66929,1.70,2
15894,5.10,10
66929,1.20,3
];
Counter:
LOAD recno() as counter
AUTOGENERATE 10
;
LEFT JOIN (Counter)
LOAD recno
RESIDENT Raw
;
LEFT JOIN (Raw)
INTERVALMATCH(counter,recno)
LOAD
start
,end
,recno
RESIDENT Raw
;
Final:
LOAD
material
,price
,recno() as no
RESIDENT Raw
ORDER BY recno
;
DROP TABLES
Raw
,Counter
;

View solution in original post

6 Replies
Not applicable
Author

Hi,

I had an idea, but I don't know if it was good.

Try:


let z1=1;

DO WHILE z1<=20
let z2=1;
Do While z2<= z1
Tab1:
LOAD * INLINE [
Qty
$(z1)
];
let z2=z2+1;
LOOP
let z1=z1+1;
LOOP


Tab1:
right join (Tab1) load * INLINE [
MatNr, Price, Qty
12, 1.29, 2
12, 1.88, 8
12, 0.45, 2
13, 12.90, 1
];



You should calculate the first table once with more numbers and store it as a QVD. Then you can load it faster.

Have Fun

Alex:)

Not applicable
Author

Thank you, Alexander. I'll work on your idea. Now I need to include a counter in the table so I can have ordered rows.

Thank you for your help and time!

johnw
Champion III
Champion III

Here's a loopless approach:

LOAD *
,recno() as no
;
LOAD
subfield(material&repeat(','&material,qty-1),',') as material
,price
INLINE [
material, price, qty
66929,1.20,3
66929,1.25,5
66929,1.70,2
15894,5.10,10
66929,1.20,3
];

johnw
Champion III
Champion III

A completely different loopless approach:

Raw:
LOAD *
,1 as start
,qty as end
,recno() as recno
INLINE [
material,price,qty
66929,1.20,3
66929,1.25,5
66929,1.70,2
15894,5.10,10
66929,1.20,3
];
Counter:
LOAD recno() as counter
AUTOGENERATE 10
;
LEFT JOIN (Counter)
LOAD recno
RESIDENT Raw
;
LEFT JOIN (Raw)
INTERVALMATCH(counter,recno)
LOAD
start
,end
,recno
RESIDENT Raw
;
Final:
LOAD
material
,price
,recno() as no
RESIDENT Raw
ORDER BY recno
;
DROP TABLES
Raw
,Counter
;

Not applicable
Author

Hi John,

I would fall on my knees 😉

I never know what these two functions are for. Great performance ...

Happy Hollydays

Alex :-)>

Not applicable
Author

Thank you very much, John. Your script does exactly what I need!

Thank you again for your time and for your help.