informix 中查询的行号

发布于 2024-07-05 12:57:35 字数 317 浏览 5 评论 0原文

我正在使用 informix 数据库,我想要一个查询,您也可以在查询中生成行号,

就像

select row_number(),firstName,lastName 
from students;

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

这里的firstName、lastName 来自数据库,其中行号是在查询中生成的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

携余温的黄昏 2024-07-12 12:57:39

使用 OLAP 表达式,您需要 OVER() 中包含某些内容,因为您不希望分区包含 SORT 子句,如下所示:

SELECT ROW_NUMBER() OVER(ORDER BY lastName, firstName) AS rn, firstName, lastName 

FROM Students;

如果您不想按名称排序,则可以使用按 ROWID 排序将记录输入系统的方式。

Using OLAP expressions you need the OVER() with something in it, since you don't want partitions include a SORT clause, like this:

SELECT ROW_NUMBER() OVER(ORDER BY lastName, firstName) AS rn, firstName, lastName 

FROM students;

and if you don't want to order by name, you could use the way records were entered in the system by ordering by ROWID.

暮倦 2024-07-12 12:57:38

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

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

小矜持 2024-07-12 12:57:38

我知道这是一个老问题,但由于我刚刚遇到这个问题并得到了这里未提及的灵魂,我很难分享它,所以这里是:

1-您需要创建一个返回给定范围内的数字的函数:

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-将此函数的结果与您想要的表相交叉:

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

唯一的事情是您必须事先知道您想要的行数,您可以使用 COUNT(*) 函数得到它。

这适用于我的 Informix 数据库,其他实现可能需要一些调整。

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.

在你怀里撒娇 2024-07-12 12:57:38

我认为最简单的方法是使用以下代码并相应地调整其返回值。
SELECT rowid, * FROM table

它对我有用,但请注意,它将返回数据库中的行号,而不是查询中的行号。

PS这是来自专家交换的公认答案。

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.

作妖 2024-07-12 12:57:37

给定一个名为 Table3 的表,其中包含 3 列:

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

注意:
seq 是表中的一个字段,具有按升序排列的唯一值。 这些数字不必是连续的。

这是返回行号(RowNum)以及查询结果的查询

SELECT table3.no, table3.seq, Table3.nm,
      (SELECT COUNT(*) FROM Table3 AS Temp
         WHERE Temp.seq < Table3.seq) + 1 AS RowNum
    FROM Table3;

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;
一身仙ぐ女味 2024-07-12 12:57:36

您可能无法在分散在多个 DBSpace 中的表中使用 ROWID,因此任何使用 ROWID 的解决方案都不是特别可移植。 这也是强烈劝阻的。

如果您的源表中没有 SERIAL 列(这是将其作为一般概念实现的更好方法),请查看
CREATE SEQUENCE,它或多或少相当于 Orrible 函数,该函数在 SELECTed 时生成唯一编号(与 SERIAL 相反,SERIAL 在插入行时生成唯一编号)。

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

守不住的情 2024-07-12 12:57:36

最好的方法是使用(新初始化的)序列。

begin work;
create sequence myseq;
select myseq.nextval,s.firstName,s.lastName from students s;
drop sequence myseq;
commit work;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文