Making the ranking query efficient

时间:2015-06-15 15:14:50

标签: sql ms-access ms-access-2010

I am using the following code in ACCESS 2010 to rank ~30000 rows from a table. However it takes around 15 minutes to do the ranking (I have to do this for 5 more columns and it could easily take more than an hour). I need these columns for further processing and hence tried creating a new table. I have also tried appending and updating to existing tables. Everything takes the same time. Am I missing something obvious here that could make it work faster?

SELECT MasterTable.Sales, (SELECT Count(*)+1 as HowMany From MasterTable AS Dupe WHERE Dupe.Sales > MasterTable.Sales) AS SalesRank INTO tableRank FROM MasterTable;

Details on the MasterTable: 11 columns. 1 primary key (Text). 5 parameters (Sales SalesQty Profit Hits Cost, all numeric). Ranks of 5 parameters(like SalesRank and so on) 30,000 rows

1 个答案:

答案 0 :(得分:0)

您是否尝试使用分组进行左连接?

SELECT
    m1.Sales,
    COUNT(*) AS HowMany
FROM
    MasterTable AS m1
    LEFT JOIN MasterTable m2 ON m1.Sales <= m2.Sales
GROUP BY
    m1.Sales