0

我在 Access 2010 中,正在尝试为以特定方式排序的查询创建排名或行号字段。我已经尝试过Rank: DCount("id","[Query1]","id <= " & [id]),但它忽略了我的查询排序,因为订单不是 ID 主键。

4

2 回答 2

1

我已经看到了您的排序逻辑,似乎我设法通过创建一个附加查询(Temp)来根据它产生一个排名,其中包含一个包含所有排序函数子产品的列。此查询的代码应如下所示:

SELECT Table1.Code1, 
    Len(Mid([Code1] & "",1,IIf(InStr([Code1] & "","*")>0,InStr([Code1] & "","*")-1,Len([Table1].[Code1] & "")))) 
    & Mid([Code1] & "",1,IIf(InStr([Code1] & "","*")>0,InStr([Code1] & "","*")-1,Len([Table1].[Code1] & ""))) & 
    Val(Mid([Code1] & "",InStr([Code1] & "","*")+1)) & 
    CStr(Val(Mid([Code1] & "",InStr([Code1] & "","*")+1))) AS OrderCode1
FROM Table1
ORDER BY Len(Mid([Code1] & "",1,IIf(InStr([Code1] & "","*")>0,InStr([Code1] & "","*")-1,Len([Table1].[Code1] & "")))), 
    Mid([Code1] & "",1,IIf(InStr([Code1] & "","*")>0,InStr([Code1] & "","*")-1,Len([Table1].[Code1] & ""))), 
    Val(Mid([Code1] & "",InStr([Code1] & "","*")+1)), 
    CStr(Val(Mid([Code1] & "",InStr([Code1] & "","*")+1))), Table1.Code1;

之后,如果您对这个额外的查询创建一个查询,并DCount在参数上使用带有“OrderCode1”列的WHERE函数,您应该得到一个顺序排名。像这样的东西:

SELECT Temp.Code1, DCount("Code1","[Temp]","OrderCode1 <= '" & [OrderCode1] & "'") AS Rank
FROM Temp;

如果您仍然需要解决方案,您可以检查它是否有效!

于 2012-09-22T18:44:10.683 回答
-1

希望这可以帮助

SELECT ROW_NUMBER() OVER (ORDER BY OrderID) AS RowNo, OrderID FROM Orders
于 2012-09-22T23:21:11.153 回答