查询显示行号为no(如序列号)

时间:2009-07-16 05:14:40

标签: ms-access

Example Data:

Col1     Col2     Col3
  a        1        x
  a        5        y
  c        8        w 
  d        9        v

结果必须是

Col1     Col2     Col3    Slno
  a        1        x       1
  a        5        y       2
  c        8        w       3
  d        9        v       4

2 个答案:

答案 0 :(得分:0)

您可以使用此查询,但col1必须是唯一字段

SELECT (select sum(1) from tab1 t where t.col1<=t1.col1 ) AS slno, t1.col1, t1.col2, t1.col3  FROM tab1 AS t1;

对于非唯一字段,您可以使用此

1)模块中的这段代码

Private curNum As Long

Public Function startNum() As Boolean
  curNum = 0
  startNum = True
End Function

Public Function GetNextNum(anyField) As Long
  curNum = curNum + 1
  GetNextNum = curNum
End Function

2)查询中的此代码

SELECT DISTINCT *, GetNextNum([AnyField]) AS MyCounter
FROM MyTable
WHERE startNum()=True;

答案 1 :(得分:0)

我正在寻找类似的要求,经过一些研究和试验,我得到了这个: -

 Update myTbl SET Slno = DCount("Col2","myTbl","Col2<='" & [Col2] & "'")

您可以使用此查询,但正如您所示,col2应该是唯一字段。