Access SQL中的前10个子查询

时间:2015-07-21 19:22:16

标签: sql ms-access

SELECT TOP 10 [FINAL_FOR_DB].[Indemnity_Paid]/[FINAL_FOR_DB].[Claim_Count] AS Indemnity_Cost, 
final_for_db.Claimant_Name, 
final_for_db.Account_Name, 
final_for_db.Claim_ID, 
final_for_db.File_Date, 
final_for_db.Resolution_Date, 
final_for_db.Claim_Status, 
final_for_db.State_Filed, final_for_db.Expense_Amount, 
final_for_db.Claim_Count, 
final_for_db.Indemnity_Paid AS [Total Indemnity]
FROM final_for_db
WHERE (((final_for_db.Account_Name)="Exxon"))
ORDER BY [FINAL_FOR_DB].[Indemnity_Paid]/[FINAL_FOR_DB].[Claim_Count] DESC;

这只会给我埃克森美孚的十大参赛作品,但我想知道是否有办法让每个帐户名称从最大的赔偿成本到最低的前10名参赛作品。我认为需要子查询。我将不胜感激任何帮助。感谢

1 个答案:

答案 0 :(得分:0)

其他RDBMS支持RANK()和ROW_NUMBER()函数。不幸的是,Access并不(据我所知)。这应该让你接近你想要的。它不能很好地处理重复(具有相同赔偿成本的两个客户将获得相同的排名,可能会让您排在前11位左右)。

Select * From 
(
    Select *
        , (
            Select count(*) 
            From final_for_db as tbl2
            where (tbl1.Indemnity_Paid/tbl1.Claim_Count) < (tbl2.Indemnity_Paid/tbl2.Claim_Count)
                and tbl1.Account_Name= tbl2.Account_Name
        ) + 1 as rank from final_for_db tbl1
) x where x.Rank < 10