[NEWSboard IBMi Forum]
  1. #1
    Registriert seit
    Feb 2007
    Beiträge
    68

    Fetch first x rows only

    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

  2. #2
    Registriert seit
    Mar 2002
    Beiträge
    5.287
    .. das geht mit den OLAP Funktionen RANK, ansonsten allenfalls mit Klimmzügen.

    D*B

    Zitat Zitat von cicero22 Beitrag anzeigen
    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
    AS400 Freeware
    http://www.bender-dv.de
    Mit embedded SQL in RPG auf Datenbanken von ADABAS bis XBASE zugreifen
    http://sourceforge.net/projects/appserver4rpg/

  3. #3
    Registriert seit
    Jan 2007
    Beiträge
    189
    In ein andere Forum von mir geschrieben:
    SQL record selection performance - Code400 -The Support Alternative

    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.
    Code:
    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.
    Code:
      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.
    mfg

    Kit
    www.ecofitonline.com
    DeskfIT - ChangefIT - XrefIT

  4. #4
    Registriert seit
    Aug 2001
    Beiträge
    2.875
    Hallo,

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

    PHP-Code:
    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
    Birgitta Hauser

    Anwendungsmodernisierung, Beratung, Schulungen, Programmierung im Bereich RPG, SQL und Datenbank
    IBM Champion seit 2020 - 4. Jahr in Folge
    Birgitta Hauser - Modernization - Education - Consulting on IBM i

Similar Threads

  1. Brauche Hilfe zu FETCH xxx USING DESCRIPTOR
    By AndreasH in forum IBM i Hauptforum
    Antworten: 13
    Letzter Beitrag: 08-05-15, 13:09
  2. FETCH n ROws in einzelne Felder einer DS
    By pedro-zapata in forum NEWSboard Programmierung
    Antworten: 6
    Letzter Beitrag: 11-09-06, 12:34
  3. Group und fetch first xx rows
    By linguin in forum IBM i Hauptforum
    Antworten: 6
    Letzter Beitrag: 04-08-06, 10:38
  4. SQL - optimize for xxx rows
    By roko in forum IBM i Hauptforum
    Antworten: 4
    Letzter Beitrag: 28-12-04, 09:45
  5. Embedded SQL - Datenbankoptionen in VARPG
    By woki in forum NEWSboard Programmierung
    Antworten: 2
    Letzter Beitrag: 13-04-04, 12:09

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • You may not post attachments
  • You may not edit your posts
  •