Row numbers for a query in informix

家住魔仙堡 提交于 2019-11-28 04:22:01

问题


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,lastName 
from students;

row_number() firstName lastName
1            john      mathew
2            ricky     pointing
3            sachin    tendulkar

Here firstName, lastName are from Database, where as row number is generated in a query.


回答1:


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;



回答2:


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).




回答3:


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;



回答4:


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.




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/119278/row-numbers-for-a-query-in-informix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!