Sql 如何在Access查询中根据排序值添加序列号
我有一个查询,返回att1的一些值。我还想在它旁边有一些值,它们表示att1的排序顺序。大概是这样的:Sql 如何在Access查询中根据排序值添加序列号,sql,ms-access,Sql,Ms Access,我有一个查询,返回att1的一些值。我还想在它旁边有一些值,它们表示att1的排序顺序。大概是这样的: att1 att2 19 3 2 2 46 4 78 5 1 1 什么样的解决方案或正确的方法可以做到这一点?我写了一篇关于各种方法的文章: 以最简单的形式: SELECT RowNumber(CStr([ID])) AS RowID, *, FROM SomeTable; 使用RowNumber函数: ' Builds conse
att1 att2
19 3
2 2
46 4
78 5
1 1
什么样的解决方案或正确的方法可以做到这一点?我写了一篇关于各种方法的文章: 以最简单的形式:
SELECT RowNumber(CStr([ID])) AS RowID, *, FROM SomeTable;
使用RowNumber函数:
' Builds consecutive row numbers in a select, append, or create query
' with the option of a initial automatic reset.
' Optionally, a grouping key can be passed to reset the row count
' for every group key.
'
' 2018-08-23. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function RowNumber( _
ByVal Key As String, _
Optional ByVal GroupKey As String, _
Optional ByVal Reset As Boolean) _
As Long
' Uncommon character string to assemble GroupKey and Key as a compound key.
Const KeySeparator As String = "¤§¤"
' Expected error codes to accept.
Const CannotAddKey As Long = 457
Const CannotRemoveKey As Long = 5
Static Keys As New Collection
Static GroupKeys As New Collection
Dim Count As Long
Dim CompoundKey As String
On Error GoTo Err_RowNumber
If Reset = True Then
' Erase the collection of keys and group key counts.
Set Keys = Nothing
Set GroupKeys = Nothing
Else
' Create a compound key to uniquely identify GroupKey and its Key.
' Note: If GroupKey is not used, only one element will be added.
CompoundKey = GroupKey & KeySeparator & Key
Count = Keys(CompoundKey)
If Count = 0 Then
' This record has not been enumerated.
'
' Will either fail if the group key is new, leaving Count as zero,
' or retrieve the count of already enumerated records with this group key.
Count = GroupKeys(GroupKey) + 1
If Count > 0 Then
' The group key has been recorded.
' Remove it to allow it to be recreated holding the new count.
GroupKeys.Remove (GroupKey)
Else
' This record is the first having this group key.
' Thus, the count is 1.
Count = 1
End If
' (Re)create the group key item with the value of the count of keys.
GroupKeys.Add Count, GroupKey
End If
' Add the key and its enumeration.
' This will be:
' Using no group key: Relative to the full recordset.
' Using a group key: Relative to the group key.
' Will fail if the key already has been created.
Keys.Add Count, CompoundKey
End If
' Return the key value as this is the row counter.
RowNumber = Count
Exit_RowNumber:
Exit Function
Err_RowNumber:
Select Case Err
Case CannotAddKey
' Key is present, thus cannot be added again.
Resume Next
Case CannotRemoveKey
' GroupKey is not present, thus cannot be removed.
Resume Next
Case Else
' Some other error. Ignore.
Resume Exit_RowNumber
End Select
End Function
所有代码也都在GitHub上:我写了一篇文章,介绍了各种方法: 以最简单的形式:
SELECT RowNumber(CStr([ID])) AS RowID, *, FROM SomeTable;
使用RowNumber函数:
' Builds consecutive row numbers in a select, append, or create query
' with the option of a initial automatic reset.
' Optionally, a grouping key can be passed to reset the row count
' for every group key.
'
' 2018-08-23. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function RowNumber( _
ByVal Key As String, _
Optional ByVal GroupKey As String, _
Optional ByVal Reset As Boolean) _
As Long
' Uncommon character string to assemble GroupKey and Key as a compound key.
Const KeySeparator As String = "¤§¤"
' Expected error codes to accept.
Const CannotAddKey As Long = 457
Const CannotRemoveKey As Long = 5
Static Keys As New Collection
Static GroupKeys As New Collection
Dim Count As Long
Dim CompoundKey As String
On Error GoTo Err_RowNumber
If Reset = True Then
' Erase the collection of keys and group key counts.
Set Keys = Nothing
Set GroupKeys = Nothing
Else
' Create a compound key to uniquely identify GroupKey and its Key.
' Note: If GroupKey is not used, only one element will be added.
CompoundKey = GroupKey & KeySeparator & Key
Count = Keys(CompoundKey)
If Count = 0 Then
' This record has not been enumerated.
'
' Will either fail if the group key is new, leaving Count as zero,
' or retrieve the count of already enumerated records with this group key.
Count = GroupKeys(GroupKey) + 1
If Count > 0 Then
' The group key has been recorded.
' Remove it to allow it to be recreated holding the new count.
GroupKeys.Remove (GroupKey)
Else
' This record is the first having this group key.
' Thus, the count is 1.
Count = 1
End If
' (Re)create the group key item with the value of the count of keys.
GroupKeys.Add Count, GroupKey
End If
' Add the key and its enumeration.
' This will be:
' Using no group key: Relative to the full recordset.
' Using a group key: Relative to the group key.
' Will fail if the key already has been created.
Keys.Add Count, CompoundKey
End If
' Return the key value as this is the row counter.
RowNumber = Count
Exit_RowNumber:
Exit Function
Err_RowNumber:
Select Case Err
Case CannotAddKey
' Key is present, thus cannot be added again.
Resume Next
Case CannotRemoveKey
' GroupKey is not present, thus cannot be removed.
Resume Next
Case Else
' Some other error. Ignore.
Resume Exit_RowNumber
End Select
End Function
所有代码也都在GitHub上:假设表名为table1,那么下面应该会产生所需的结果:
select a.att1, (select count(*) from table1 b where b.att1 <= a.att1) as att2
from table1 a;
对于每个记录,查询计算小于或等于当前记录的记录数,然后将其作为排序索引输出。假设表名为table1,则以下操作应产生所需的结果:
select a.att1, (select count(*) from table1 b where b.att1 <= a.att1) as att2
from table1 a;
对于每个记录,查询计算小于或等于当前记录的记录数,然后将其输出为排序索引。非常感谢您的帮助,但Lee Mac解决方案似乎更简单,而且有效。是的,它适用于少数记录,但如果您有许多记录,这将是非常缓慢的。所需的SQL尽可能简单:一行。请参阅编辑后的答案。非常感谢您的帮助,但Lee Mac解决方案似乎更简单,而且有效。是的,它只适用于少数记录,但如果您有很多记录,它将非常缓慢。所需的SQL尽可能简单:一行。请看编辑后的答案。