如何从ms访问中的查询中选择最后一组行

时间:2016-03-19 23:46:36

标签: sql database ms-access-2010

我正在尝试运行两个查询,一个选择前22行,另一个选择查询的剩余行。我能够选择前22行。但现在我需要选择接下来的22行。基本上我有两个访问报告,一个显示前22行,下一个将显示接下来的22行。任何帮助将不胜感激。有没有人知道可以让我朝着正确方向发送的功能?

到目前为止,我的查询是选择前22行:

SELECT TOP 22[UB-04_line_items].client_id, [UB-04_line_items].revenue_code, Revenue_Codes.rev_code_desc, [UB-04_line_items].total_chgs, [UB-04_line_items].cpt_code, [UB-04_line_items].service_units, [UB-04_line_items].service_date, [UB-04_line_items].total_chgs, Sum(IIf(IsNull([reason_code])=False,[disputed_amount],0)) AS [AMT DISPUTED], GetList("Select Distinct reason_code From [Itemized_statements] As T1 Where t1.reason_code <> NULL AND  t1.client_id = " & [Itemized_statements].client_id & " AND T1.revenue_code = " & [Itemized_statements].revenue_code & "","",", ") AS [Err Code]
FROM [UB-04_line_items] INNER JOIN (Itemized_Statements LEFT JOIN Revenue_Codes ON Itemized_Statements.revenue_code = Revenue_Codes.revenue_code) ON [UB-04_line_items].client_id = Itemized_Statements.client_id
GROUP BY [UB-04_line_items].client_id, [UB-04_line_items].revenue_code, Revenue_Codes.rev_code_desc, [UB-04_line_items].cpt_code, [UB-04_line_items].service_units, [UB-04_line_items].service_date, [UB-04_line_items].total_chgs, [UB-04_line_items].total_chgs, Itemized_Statements.client_id, Itemized_Statements.revenue_code
HAVING ((([UB-04_line_items].client_id)=[Itemized_Statements].[client_id]) AND ((Itemized_Statements.client_id)=[forms]![frmClients]![client_ID]) AND ((Itemized_Statements.revenue_code)=[UB-04_line_items].[revenue_code]))
ORDER BY Itemized_Statements.client_id;

1 个答案:

答案 0 :(得分:0)

通过运行client_idrevenue_code的计数来考虑使用计算的行号(假设此对在聚合查询中是唯一的)。然后,在WHERE子句中使用此子查询来调整所需的范围。请务必从存储的查询中删除TOP子句。

前22名 (将Qry替换为实际存储的查询名称)

SELECT (SELECT Count(*) 
        FROM Qry sub 
        WHERE sub.client_id < Qry.client_id
            OR (sub.client_id = Qry.client_id
                AND sub.revenue_code <= Qry.revenuce_code)) As RowNo, *
FROM Qry
WHERE (SELECT Count(*) 
        FROM Qry sub 
        WHERE sub.client_id < Qry.client_id
            OR (sub.client_id = Qry.client_id
                AND sub.revenue_code <= Qry.revenuce_code)) <= 22

下一页22 (将Qry替换为实际查询名称)

SELECT (SELECT Count(*) 
        FROM Qry sub 
        WHERE sub.client_id < Qry.client_id
            OR (sub.client_id = Qry.client_id
                AND sub.revenue_code <= Qry.revenuce_code)) As RowNo, *
FROM Qry
WHERE (SELECT Count(*) 
        FROM Qry sub 
        WHERE sub.client_id < Qry.client_id
            OR (sub.client_id = Qry.client_id
                AND sub.revenue_code <= Qry.revenuce_code)) BETWEEN 23 AND 44