在MS Access中实现ROW_NUMBER / PARTITION BY

时间:2014-02-20 19:29:02

标签: ms-access ms-access-2010

如何通过MS访问来实现分区上的行号。我可以找到谷歌,但是找不到信息可以请你怎么做

RowNumber Over(Partition by city Order By EmployeeID)

我的数据看起来像这样

DOC_TYPE    Ino
3a  1800xxc1
3b  1810xxc2
3c  1700xxc3
3a  1700xxc4
3a  1800xxc5
3a  1800xxc6
3b  1800xxc7

我需要像这样

DOC_TYPE    Ino Seq
3a  1800xxc1    1
3a  1700xxc4    2
3a  1800xxc5    3
3a  1800xxc6    4
3b  1810xxc2    1
3b  1800xxc7    2
3c  1700xxc3    1

这是我的查询

SELECT t1.RT_TAXCODE, t1.INV_NO, COUNT(*) AS Sno
FROM GroupByTAXCODE AS t1 INNER JOIN GroupByTAXCODE AS t2 ON (t2.RT_TAXCODE = t1.RT_TAXCODE) AND (t2.Inv_no <= t1.Inv_no)
GROUP BY t1.RT_TAXCODE, t1.INV_NO
HAVING COUNT(*)=1
ORDER BY 1, 3;

这花费更多时间为30秒

1 个答案:

答案 0 :(得分:9)

在许多情况下,我们可以通过在表上执行不等的自联接并聚合结果来获得类似的结果。例如,对于名为[MyData]

的表中的数据
Ino  TYPE      DOC
---  --------  ---
  1  1800xxc1  3a 
  2  1810xxc2  3b 
  3  1700xxc3  3c 
  4  1700xxc4  3a 
  5  1800xxc5  3a 
  6  1800xxc6  3a 
  7  1800xxc7  3b 

查询

SELECT 
    t1.DOC,
    t1.TYPE,
    COUNT(*) AS [Ino Seq]
FROM 
    MyData AS t1
    INNER JOIN
    MyData AS t2
        ON t2.DOC = t1.DOC
            AND t2.Ino <= t1.Ino
GROUP BY
    t1.DOC,
    t1.TYPE
ORDER BY 1, 3

返回

DOC  TYPE      Ino Seq
---  --------  -------
3a   1800xxc1        1
3a   1700xxc4        2
3a   1800xxc5        3
3a   1800xxc6        4
3b   1810xxc2        1
3b   1800xxc7        2
3c   1700xxc3        1