MS Access密集等级

时间:2016-12-07 21:55:55

标签: sql ms-access access rank dense-rank

所以我试图找到一种方法来复制MS Access表上的denserank()函数。我发现以下查询有很多帮助:

SELECT 
  Table1.ColA
  ,( SELECT Count(*) 
     FROM
       Table1 AS T 
     WHERE
       T.ColA = Table1.ColA AND
       T.PrimaryKeyFieldName <= Table1.PrimaryKeyFieldName) AS ColB;

此查询非常适合复制rank()函数。但是,我想修改此查询,以便它复制denserank()。换句话说,组的排名现在看起来像这样:

等级

0
0
0
3
3
3
6
6
6

相反,我正在寻找的是:

等级

0
0
0
1
1
1
2
2
2

我想要实现的另一个例子可以在例子#3中看到:

https://support.microsoft.com/en-us/kb/208946

任何想法都将不胜感激!提前谢谢!

1 个答案:

答案 0 :(得分:2)

我相信你只想让RANK从零开始递增,每组递增1?如果是这样,试试这个:

select T1.*,
   (select count(*)
    from (select distinct T2.flda
          from table1 as T2
         )  as T2
    where T2.flda <= T1.flda
   ) as dense_rank
From table1 as T1;

这是我之前提到的第二个查询。但是,我不知道这是否会为您提供所需的分组。

SELECT T1.*, 
   (select count(*)
    from (select distinct T2.flda, T2.fldb, T2.MyDate
          from table1 as T2
         )  as T2
    where (T2.flda <= T1.flda) AND (T2.MyDate <> T1.MyDate)
) AS dense_rank
FROM table1 AS T1;