我有4个表,我可以在select查询中通过联接帮助选择数据...我想要获取每个记录的序列号(行号)。首先获取的记录应该是1,接下来的2等等......
在oracle中,RowNum中的等等。
答案 0 :(得分:4)
Brettski的答案是ASP风格,需要大量编辑。
SELECT DCOUNT("YourField","YourTable","YourField <= '" & [counter] & "'")
AS RowNumber,
YourField as counter FROM YourTable;
以上是您的基本语法。您可能会发现此运行非常慢。我的典型解决方案是带有自动编号字段的存储桶表。这似乎很糟糕,但它让我控制,可能在这种情况下,它允许速度。
答案 1 :(得分:0)
在Access中使用已排序的Make Table Query,我使用以下内容(如果您查看Query将无法工作,因为当您不希望它时,这会增加数字)....
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
答案 2 :(得分:-1)
使用下表
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
此方法存在的问题是,如果结果集中存在重复项,则计数将会关闭。
这个article很好地解释了如何在查询中添加行计数列。