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

分组排序的问题
在mssql中有数据库A和B。分别有表结构如下

查询条件:
A.a.outdate>='20140201';A.a.price>0
对sum(A.a.goods_nb)进行排序,A.a.goods_no进行分组
然后连接B.b查询出goods_pic
求个sql语句实现以上查询
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

仿照你这个写的


select colthno,sum(nb) nb

引用:
仿照你这个写的


得修改一下,你的应该会报错:

declare @page_size int;
declare @page_num int;

--比如:每页10条记录
set @page_size = 10;

--比如:先取第1页
set @page_num = 1;

select *
from
(
select *,
   (row_number() over(order by s_nb) - 1) / @page_size as rownum
from
(
select SUM(nb) as s_nb,colthno
 from cmd_xjjx_sale
 where outdate<='20140223' and outdate>='20140216' and endprice>0
 group by colthno
)a
inner join BI数据仓库.dbo.BI_colthno_kind b
        on a.colthno=b.colthno

)t
where rownum = @page_num - 1


的确报错了
消息 8156,级别 16,状态 1,第 22 行
多次为 't' 指定了列 'colthno'。


再修改一下:
declare @page_size int;
declare @page_num int;

--比如:每页10条记录
set @page_size = 10;

--比如:先取第1页
set @page_num = 1;

select *
from
(
select a.s_nb,
       b.*,
       (row_number() over(order by a.s_nb) - 1) / @page_size as rownum
from
(
 select SUM(nb) as s_nb,colthno
 from cmd_xjjx_sale
 where outdate<='20140223' and outdate>='20140216' and endprice>0
 group by colthno
)a
inner join BI数据仓库.dbo.BI_colthno_kind b
        on a.colthno=b.colthno

)t
where rownum = @page_num - 1