2016-03-19 150 views
0

我想運行兩個查詢,一個選擇前22行,另一個選擇查詢的其餘行。我可以選擇前22行。但現在我需要選擇接下來的22行。基本上我有2個訪問報告,一個顯示前22行,下一個顯示下22行。任何幫助將不勝感激。有沒有人知道一個功能可以讓我朝着正確的方向發展?如何從ms查詢中選擇最後一組行查詢

這是迄今爲止我的查詢選擇最上面的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; 
+2

有一個類似問題與答案在這裏:http://stackoverflow.com/questions/10455746/select-records-skipping-rows-in-ms-access –

+0

client_id在此查詢或其他字段中是唯一列嗎? – Parfait

回答

0

考慮使用一個計算行數通過運行client_id和計(假設這對處於彙總查詢唯一)。然後,在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