问题补充说明:OVER(order by xxx) 这里的xxx必须是select 中的列名,不能用别名。 如果我select的字段中包含一个子查询如:select a.name as name ,(select sum(b.qty) from b where a.name = b.name) as qty from a group by name。 用以上方式进行分页查询时不能用OVE... OVER(order by xxx) 这里的xxx必须是select 中的列名,不能用别名。 如果我select的字段中包含一个子查询如:select a.name as name ,(select sum(b.qty) from b where a.name = b.name) as qty from a group by name。 用以上方式进行分页查询时不能用OVER(order by qty),只能order by 那段子查询,比较麻烦。请问有解决办法么?(由于使用的框架、需求的限制,语句比较复杂,没法优化) 展开
只能做一个嵌套,按照你的,我建了几个表和查询,你看看,符合你要求不
建表
cr360问答eate table a (坐后id int,name varchar(10));
故运际黑断外铁应积未create table b (
name varchar(10),
qty int);
插入数据
insert into a values (1,'张三')insert into a valu派浓早手es (2,'李四')
insert into a values (3,'王五')
insert into a values (4,'赵六')
insert i厚歌密没区研nto a valu汽殖核es (5,'孙七')
insert into b values ('张三',100)
inse展企rt into b values ('张三',200)
insert into b values ('李四',500)
insert into b values ('王五',100)
insert into b values ('王五',200)
inse粮动求去航小机实减rt into b value既毫率s ('王五',500)
运行语句
select t.name,t.qty,row_number() over (order by t.qty)from
(select a.name,sum(b.qty) qty
from a left join b on a.name=b.name group by a.name) t
结果截图
标签:sqlserver