Use Access SQL to do a grouped ranking

后端 未结 6 705
悲哀的现实
悲哀的现实 2020-11-29 12:26

How do I rank salespeople by # customers grouped by department (with ties included)?

For example, given this table, I want to create the Rank column on the

6条回答
  •  旧巷少年郎
    2020-11-29 13:06

    Great solution with subquery! Except for huge recordsets, the subquery solution gets very slow. Its better(quicker) to use a Self JOIN, look at the folowing solution: self join

    SELECT tbl1.SalesPerson , count(*) AS Rank 
    FROM tbl AS tbl1 INNER JOIN tbl AS tbl2 ON tbl1.DEPT = tbl2.DEPT 
        AND tbl1.#Customers < tbl2.#Customers 
    GROUP BY tbl1.SalesPerson 
    

提交回复
热议问题