2014-07-23 73 views
0

我有以下的MS Access查詢,我想它返回按名稱排序的結果和「分頁」的「僞造」一個ROWNUMBER尋呼和訂購MS Access查詢

select * from (SELECT * 
FROM (SELECT 
     s.name as SHolderCategory, 
     c1.id, 
     c1.fmember, 
     c1.link, 
     m.name as category, 
     c1.name, 
     c1.address1, 
     c1.address2, 
     c1.city, 
     c1.state, 
     c1.zip, 
     (SELECT COUNT(c2.id) FROM orgs AS c2 WHERE c2.id <= c1.id) AS rownumber     
FROM 
((orgs AS c1 inner join membershipcls m on m.Id = c1.mClassID) 
inner join SHolderscategories s on s.Id = c1.SHolderCategoryID 
) 
where c1.active = 1) 
    order by c1.name) 
WHERE rownumber > 20 AND rownumber <=40 

這裏的問題是,排序在執行分頁的where子句之前完成。 所以它最終一次排序一個頁面,而不是排序整個結果集然後分頁......所以結果是錯誤的,因爲在頁面1中我有名字以a開頭,然後在第2頁它出現回到以c開頭的名字....等等

當我嘗試獲取訂單子句,以便查詢首先執行分頁...先生ACCESS是憤怒!並告訴我這是一個複雜的查詢!

對此的任何解決方法?

+0

這應該是有用http://stackoverflow.com/q/1900635/2448686 – ashareef

回答

2

也試試這個方法:

SELECT * FROM 
(
    SELECT TOP 20 * 
    FROM 
    (
     SELECT TOP 40 
      s.name as SHolderCategory, 
      c1.id, 
      c1.fmember, 
      c1.link, 
      m.name as category, 
      c1.name, 
      c1.address1, 
      c1.address2, 
      c1.city, 
      c1.state, 
      c1.zip 
     FROM 
      orgs AS c1 
      inner join membershipcls m on m.Id = c1.mClassID 
      inner join SHolderscategories s on s.Id = c1.SHolderCategoryID 
     WHERE c1.active = 1 
     ORDER BY c1.name 
    ) o 
    ORDER BY o.name DESC 
) f ORDER BY f.name