Qlik Community

QlikView Scripting

Discussion Board for collaboration on QlikView Scripting.

QlikView Fans! We’d love to hear from you.
Share your QlikView feedback with the product team… Click here to participate in our 5-minute survey.
Rules, plus terms and conditions, can be found here.
Not applicable

slow for...next, how to get top n records?

Hi everyone...

I have this situation inside a script.

I will explain the scenario with a simple (yet not logical) example, since the original it's too complicate..

I have a table with 4 columns, A (products), B (sites), C (total sales), D (ranking, build by hand after ordered by total sales).

The problem I'm facing is that I need to get products from sites, ordered by the ranking, but for each different sites.

In the original data, this table has 2m rows, and B has at least 300 unique values.

So, what I need, is get the first 400 rows where B is equal to 1, the first 400 rows where B is equal to 2, and so on until B equals 100 (this is not exactly this way, B has leaps, I have a table that show the code already loaded).

Then I have to get the first 300 rows where B is equal to 101, and so on...

This goes, for every each value of B..

My code rigth now, includes a nice for... each reading the table of values from B and looking for the quantity of records needed in the original table.

This is too slow, last run takes about 40 min.

Ok, maybe it's not slow as hell, but I'm sure there is a better way to accomplish it.

Thank you for reading.


2 Replies
Valued Contributor II

slow for...next, how to get top n records?

Hi, gonzalo,

the top first records you get with:


First 400

LOAD * From ...;

Greetings from Munich



Not applicable

Re: slow for...next, how to get top n records?

Yes yes, sorry About that, I already have this code. Maybe I not explain myself good, Or I forget a part..

The code I have rigth now, is this:

for i=0 to $(rows)

LET ag = Peek('dep',$(i),'t_iter');


First 400






B = $(ag);


this code, is the slow one.. the problem is doing this for.

Community Browser