日期:2014-05-16  浏览次数:20557 次

分页查询后排序不正常的问题
sql如下
declare @page_size int;
declare @page_num int;
set @page_size='10';
set @page_num='1'

select * from
(
select 
colthno,Quantity_sold,bcolthno,sum_order,rkcolthno,sum_deli,
(row_number() over (order by Quantity_sold desc)-1)/@page_size as rownum 
from
(
select * from
(
select * from
(
select b.colthno,sum(s.nb) Quantity_sold  from CMD..cmd_xjjx_sale s ,BI_Product_Information b
where s.colthno=b.colthno
and b.classname8='13FW'
and s.endprice>0
group by b.colthno
) a 
left join 
(
select  b1.colthno bcolthno,sum(order_quantity) sum_order
from BI_order bo,BI_Product_Information b1
where b1.classname8='13FW'
and bo.colthno=b1.colthno
group by b1.colthno
)b
on a.colthno=b.bcolthno
)x left join
(select  b1.colthno rkcolthno,SUM(rk_nb) sum_deli
from BI_Product_Information b1,BI_RK rk1
where b1.classname8='13FW'
and b1.colthno=rk1.colthno
group by b1.colthno
)y
on x.colthno=y.rkcolthno
) t
)t2
where rownum = @page_num - 1

我想按照Quantity_sold 来排序输出但查询的结果排序既不是升序也不是降序,请大神们看看是什么原因
------解决方案--------------------
在整个语句里面加上ORDER BY 来排序吧。
------解决方案--------------------
你要想查出来的是按Quantity_sold 排序,必须最终的结果的地方要再按Quantity_sold 排一次序,而不是像你写的只在开始按次排序过。
------解决方案--------------------
在语句后面加上 order by Quantity_sold desc 试试。
------解决方案--------------------
你在最外层进行序号化