PDA

View Full Version : Fetch first x rows only



cicero22
14-08-09, 08:32
Hallo Forum.

Ich möchte die SQL Funktion "Fetch first x rows only" anwenden - aber quasi pro Gruppenwechsel.

D.h. ich möchte nicht nur x Rows allgemein haben - sondern eben pro angezeigten Gruppenwechsel im SQL möchte ich die z.b. 10 ersten Artikel pro Kunde haben.

Geht sowas?

Danke cicero

BenderD
14-08-09, 10:15
.. das geht mit den OLAP Funktionen RANK, ansonsten allenfalls mit Klimmzügen.

D*B


Hallo Forum.

Ich möchte die SQL Funktion "Fetch first x rows only" anwenden - aber quasi pro Gruppenwechsel.

D.h. ich möchte nicht nur x Rows allgemein haben - sondern eben pro angezeigten Gruppenwechsel im SQL möchte ich die z.b. 10 ersten Artikel pro Kunde haben.

Geht sowas?

Danke cicero

kitvb1
14-08-09, 11:41
In ein andere Forum von mir geschrieben:
SQL record selection performance - Code400 -The Support Alternative (http://www.code400.com/forum/showthread.php?t=7749)

You could do a multiple row fetch into an array DS. Then process the array. This depends on the size of your fetched rows as SQL always tries to fetch 32k blocks. So the shorter your fetched row, the more rows you can fetch, obviously also within array limitations. The max is 32767 rows in one fetch statement. Using an MR fetch does not affect the way you define your cursor. You could also check how many rows and the last row fetched from the diagnostics area.

Just a note on the code below. The SFLSIZE is 500 and contains only library names i.e. 500 lines of 6 colums = 30k = just under 1 block. I use "select *..." in the above statement as the there is only 1 field (whlib) in the file. If you have many fields, it is better & faster to use "select myfield1, myfield2...", not the asterisk.

exec sql
DECLARE XLibCsr cursor for
Select * from XREF02
order by whlib;
Here's the fetch, from the diagnostics area you can also check the nr of rows and the last row fetched. In this case, I didn't need to.

exec sql
fetch next from XLibCsr
for :dArrSize rows into :LbArrScn;
clear x;
SflRowNr = 1;

// Now build the SFL from the array
// build the row
For x = SflRowNr to dArrSize;
clear DspFld;

// build the column
LNamArr = %subarr(LbArrScn :SflRowNr :6);
...

Kombiniert mit was BenderD schreibte, kannst Du was schönes bauen.

B.Hauser
14-08-09, 13:00
Hallo,

die OLAP Ranking Funktionen reichen aus, sofern man bereits auf Release V5R4 oder höher ist.


With x as (Select Row_Number() Over(Partition By Kunde) as Rang,
a.*
from MyTable a
Where ...)
Select *
From x where Rang < 10

Anmerkung: Das Ergebnis einer OLAP-Ranking-Funktion kann man nur mit Hilfe einer Common Table Expression oder eines Sub-Selects selektieren.

Birgitta