15

我在 Microsoft Access 中有一个表,我想在 Access 中使用选择查询在列中显示行号,就像ROW_NUMBER()在 SQL Server 中使用函数一样。

在 SQL Server 中,我可以使用以下查询:

SELECT ROW_NUMBER() OVER (ORDER BY tblUser.UserID) AS NoRow, * 
FROM tblUser

我在访问中使用相同的查询,但出现错误。

你能帮助我吗?

4

3 回答 3

25

你可以试试这个查询:

Select A.*, (select count(*) from Table1 where A.ID>=ID) as RowNo
from Table1 as A
order by A.ID
于 2013-07-25T03:02:12.053 回答
4

使用 MS Access 执行此操作的一种方法是使用子查询,但它没有任何类似的功能:

SELECT a.ID, 
       a.AText, 
       (SELECT Count(ID) 
        FROM table1 b WHERE b.ID <= a.ID 
        AND b.AText Like "*a*") AS RowNo
FROM Table1 AS a
WHERE a.AText Like "*a*"
ORDER BY a.ID;
于 2013-02-04T10:23:09.803 回答
1

通过 VB 函数:

Dim m_RowNr(3) as Variant
'
Function RowNr(ByVal strQName As String, ByVal vUniqValue) As Long
' m_RowNr(3)
' 0 - Nr
' 1 - Query Name
' 2 - last date_time
' 3 - UniqValue

If Not m_RowNr(1) = strQName Then
  m_RowNr(0) = 1
  m_RowNr(1) = strQName
ElseIf DateDiff("s", m_RowNr(2), Now) > 9 Then
  m_RowNr(0) = 1
ElseIf Not m_RowNr(3) = vUniqValue Then
  m_RowNr(0) = m_RowNr(0) + 1
End If

m_RowNr(2) = Now
m_RowNr(3) = vUniqValue
RowNr = m_RowNr(0)

End Function

用法(无排序选项):

SELECT RowNr('title_of_query_or_any_unique_text',A.id) as Nr,A.*
From table A
Order By A.id

如果需要排序或多个表连接,则创建中间表:

 SELECT RowNr('title_of_query_or_any_unique_text',A.id) as Nr,A.*
 INTO table_with_Nr
 From table A
 Order By A.id
于 2018-01-09T12:38:29.577 回答