在 MS Access 上返回行号

发布于 2024-08-10 00:58:55 字数 120 浏览 4 评论 0原文

我有 4 个表,我在选择查询中的联接的帮助下从中选择数据...我想要每个记录在获取时有一个序列号(行号)。第一个获取的记录应该是 1,下一个是 2,依此类推...

在 oracle 中,RowNum 中的等值。

I have 4 tables, from which i select data with help of joins in select query...I want a serial no.(row number) per record as they are fetched. first fetched record should be 1, next 2 and so on...

In oracle the equiavelent in RowNum.

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

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

发布评论

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

评论(3

向日葵 2024-08-17 00:58:55

Brettski 的答案是 ASP 风格的,需要大量编辑。

SELECT DCOUNT("YourField","YourTable","YourField <= '" & [counter] & "'") 
  AS RowNumber,  
  YourField as counter FROM YourTable;  

以上是您的基本语法。您可能会发现它运行得很慢。我的典型解决方案是带有自动编号字段的存储桶表。这看起来很笨拙,但它给了我控制权,并且可能在这种情况下它允许速度。

The answer by Brettski is ASP flavored and would need a lot of editing.

SELECT DCOUNT("YourField","YourTable","YourField <= '" & [counter] & "'") 
  AS RowNumber,  
  YourField as counter FROM YourTable;  

Above is your basic syntax. You are likely to find this runs very slow. My typical solution is a bucket table with Autonumber field. That seems kludgy, but it gives me control and probably in this case it allows speed.

寄风 2024-08-17 00:58:55

在 Access 中使用排序的 Make Table 查询,我使用以下命令(如果您查看查询,则不会工作,因为当您不希望它增加时,这会增加数字)....

setRowNumber 'resetting increment before running SQL
DoCmd.RunSQL ... , rowNumber([Any Field]) AS ROW, ...

'Increment Number: Used to create temporary sorted Table for export
Private ROWNUM As Long

'dummyField: must take an input to update in Query
Public Function rowNumber(ByVal dummyField As Variant, Optional ByVal incBy As Integer = 1) As Long
    ROWNUM = ROWNUM + incBy 'increments before value is returned
    rowNumber = ROWNUM
End Function

Public Function setRowNumber(Optional ByVal setTo As Long = 0) As Long
    ROWNUM = setTo
    setRowNumber = ROWNUM
End Function

Using a sorted Make Table Query in Access, I use the following (wouldn't work if you look at the Query, as that would increment the number when you don't want it to)....

setRowNumber 'resetting increment before running SQL
DoCmd.RunSQL ... , rowNumber([Any Field]) AS ROW, ...

'Increment Number: Used to create temporary sorted Table for export
Private ROWNUM As Long

'dummyField: must take an input to update in Query
Public Function rowNumber(ByVal dummyField As Variant, Optional ByVal incBy As Integer = 1) As Long
    ROWNUM = ROWNUM + incBy 'increments before value is returned
    rowNumber = ROWNUM
End Function

Public Function setRowNumber(Optional ByVal setTo As Long = 0) As Long
    ROWNUM = setTo
    setRowNumber = ROWNUM
End Function
初懵 2024-08-17 00:58:55

通过下表,

SET NOCOUNT ON 

CREATE TABLE people 
( 
    firstName VARCHAR(32), 
    lastName VARCHAR(32) 
) 
GO 

INSERT people VALUES('Aaron', 'Bertrand') 
INSERT people VALUES('Andy', 'Roddick') 
INSERT people VALUES('Steve', 'Yzerman') 
INSERT people VALUES('Steve', 'Vai') 
INSERT people VALUES('Joe', 'Schmoe')

您可以使用子查询来创建计数行:

SELECT 
    rank = COUNT(*), 
    a.firstName, 
    a.lastName 
FROM 
    people a  
    INNER JOIN people b 
    ON  
        a.lastname > b.lastname 
        OR 
        ( 
            a.lastName = b.lastName 
            AND 
            a.firstName >= b.firstName 
        ) 
GROUP BY 
    a.firstName, 
    a.lastName 
ORDER BY 
    rank

此方法的问题是,如果结果集中存在重复项,计数将会关闭。

这篇文章很好地解释了如何向查询添加行计数列。

With the following table

SET NOCOUNT ON 

CREATE TABLE people 
( 
    firstName VARCHAR(32), 
    lastName VARCHAR(32) 
) 
GO 

INSERT people VALUES('Aaron', 'Bertrand') 
INSERT people VALUES('Andy', 'Roddick') 
INSERT people VALUES('Steve', 'Yzerman') 
INSERT people VALUES('Steve', 'Vai') 
INSERT people VALUES('Joe', 'Schmoe')

You can use a sub query to create the counting row:

SELECT 
    rank = COUNT(*), 
    a.firstName, 
    a.lastName 
FROM 
    people a  
    INNER JOIN people b 
    ON  
        a.lastname > b.lastname 
        OR 
        ( 
            a.lastName = b.lastName 
            AND 
            a.firstName >= b.firstName 
        ) 
GROUP BY 
    a.firstName, 
    a.lastName 
ORDER BY 
    rank

The problem with this method is that the count will be off if there are duplicates in your results set.

This article explains pretty well how to add a row counting column to your query.

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