很棘手的分页SQL语句中的ORDER BY clause (Item) conflicts with DISTINCT
近日改一程序,数据库是ACCESS,要求分页.
1. 有唯一主键Job_No,非自动增长列
2. 排序字段:--〉单据状态(StatusNo),出单时间,生成单据时间排序.
3. 一个单据同一时间只有一种状态.
问题出在我用这个来排序时,产生ORDER BY clause ( StatusNo ) conflicts with DISTINCT. 这是因为多个单据拥有相同状态造成的.
各位高手有什么好的方法应对ACCESS下的单纯用SQL语句分页.
数据库已经不能更改,只能更改查询语句.之前是全部查出后用记录集提取分页.
这种多表多字段查询及排序的分页,又是在ACCESS.
网上查询的答案都不能给出一个正确的结果,除非不用distinct.但不用结果集里就有重复.
//生成的例句 (没有带DISTINCT,带了就会出错)
select * FROM
( select top 15 Job_NO,C_ID,C.Customer_Name,StatusNo,Pay_Date,Create_Date FROM EServices,Customers C where Job_No not in
( select top 30 Job_No FROM EServices,Customers C where (1> 0) and C.ID= EServices.C_ID Order by StatusNo,Pay_Date,Create_Date DESC
)
and C.ID= EServices.C_ID order by StatusNo,Pay_Date,Create_Date DESC
)
as TempTB order by StatusNo,Pay_Date,Create_Date ASC
------解决方案--------------------先select出来后再distict呢。
------解决方案--------------------hehe,
select * 之后,或存入临时表在处理阿。
------解决方案--------------------select distinct * from
(
--你的代码
select * FROM
( select top 15 Job_NO,C_ID,C.Customer_Name,StatusNo,Pay_Date,Create_Date FROM EServices,Customers C where Job_No not in
( select top 30 Job_No FROM EServices,Customers C where (1> 0) and C.ID= EServices.C_ID Order by StatusNo,Pay_Date,Create_Date DESC
)
and C.ID= EServices.C_ID order by StatusNo,Pay_Date,Create_Date DESC
)
as TempTB order by StatusNo,Pay_Date,Create_Date ASC
--
)