Row numbers for a query in informix

后端 未结 6 2058
無奈伤痛
無奈伤痛 2020-12-19 11:15

I am using informix database, I want a query which you could also generate a row number along with the query

Like

select row_number(),firstName,lastN         


        
相关标签:
6条回答
  • 2020-12-19 11:29

    select sum(1) over (order by rowid) as row_number, M.* from systables M

    0 讨论(0)
  • 2020-12-19 11:34

    I think the easiest way would be to use the following code and adjust its return accordingly. SELECT rowid, * FROM table

    It works for me but please note that it will return the row number in the database, not the row number in the query.

    P.S. it's an accepted answer from Experts Exchange.

    0 讨论(0)
  • 2020-12-19 11:38

    The best way is to use a (newly initialized) sequence.

    begin work;
    create sequence myseq;
    select myseq.nextval,s.firstName,s.lastName from students s;
    drop sequence myseq;
    commit work;
    
    0 讨论(0)
  • 2020-12-19 11:38

    You may not be able to use ROWID in a table that's fragmented across multiple DBSpaces, so any solution that uses ROWID is not particularly portable. It's also strongly discouraged.

    If you don't have a SERIAL column in your source table (which is a better way of implementing this as a general concept), have a look at CREATE SEQUENCE, which is more or less the equivalent of an Orrible function that generates unique numbers when SELECTed from (as opposed to SERIAL, which generates the unique number when the row is INSERTed).

    0 讨论(0)
  • 2020-12-19 11:44

    Given a table called Table3 with 3 columns:

    colnum  name   datatype
    ======= =====  ===
    1       no     text;
    2       seq    number;
    3       nm     text;
    

    NOTE: seq is a field within the Table that has unique values in ascending order. The numbers do not have to be contiguous.

    Here is query to return a rownumber (RowNum) along with query result

    SELECT table3.no, table3.seq, Table3.nm,
          (SELECT COUNT(*) FROM Table3 AS Temp
             WHERE Temp.seq < Table3.seq) + 1 AS RowNum
        FROM Table3;
    
    0 讨论(0)
  • 2020-12-19 11:50

    I know its an old question, but since i just faced this problem and got a soultion not mentioned here, i tough i could share it, so here it is:

    1- You need to create a FUNCTION that return numbers in a given range:

    CREATE FUNCTION fnc_numbers_in_range (pMinNumber INT, pMaxNumber INT)
    RETURNING INT as NUMERO;
    DEFINE numero INT;
    LET numero = 0;
    FOR numero = pMinNumber TO pMaxNumber   
        RETURN numero WITH RESUME;  
    END FOR;    
    END FUNCTION; 
    

    2- You Cross the results of this Function with the table you want:

    SELECT * FROM TABLE (fnc_numbers_in_range(0,10000)), my_table;
    

    The only thing is that you must know before-hand the number of rows you want, you may get this with the COUNT(*) Function.

    This works with my Informix Database, other implementations may need some tweaking.

    0 讨论(0)
提交回复
热议问题