SQL 分组排序分页(大神帮写的膜拜一下)

查询全部:

SELECT P3.ID, P3.Name, P3.AddTime
FROM (SELECT Name, MAX(AddTime) AS MaxAddTime
FROM Product AS P1
GROUP BY Name) AS P2
INNER JOIN Product AS P3 ON P2.Name = P3.Name
ORDER BY P2.MaxAddTime DESC, P3.AddTime DESC, P3.ID DESC

正反TOP方法的分页(每页5行,查第5页,即第21-25行数据)
(1)按要求的顺序取前25行,(2)颠倒过来取前5行,(3)再把顺序颠倒回来

SELECT P5.ID, P5.Name, P5.MaxAddTime
FROM
(SELECT TOP 5 P4.*
FROM
(SELECT TOP 25 P3.ID, P3.Name, P3.AddTime, P2.MaxAddTime
FROM
(SELECT Name, MAX(AddTime) AS MaxAddTime
FROM Product AS P1
GROUP BY Name) AS P2
INNER JOIN Product AS P3 ON P2.Name = P3.Name
ORDER BY P2.MaxAddTime DESC, P3.AddTime DESC, P3.ID DESC) AS P4
ORDER BY P4.MaxAddTime, P4.AddTime, P4.ID) AS P5
ORDER BY P5.MaxAddTime DESC, P5.AddTime DESC, P5.ID DESC

ROW_NUMBER方法(适用于SQL Server 2005以上)
(每页5行,查第5页,即第21-25行数据)

SELECT P4.ID, P4.Name, P4.AddTime
FROM
(SELECT P3.ID, P3.Name, P3.AddTime, ROW_NUMBER() OVER (ORDER BY P2.MaxAddTime DESC, P3.AddTime DESC, P3.ID DESC) AS RN
FROM
(SELECT Name, MAX(AddTime) AS MaxAddTime
FROM Product AS P1
GROUP BY Name) AS P2
INNER JOIN Product AS P3 ON P2.Name = P3.Name) AS P4
WHERE P4.RN BETWEEN 21 AND 25

时间: 2024-10-28 16:23:07

SQL 分组排序分页(大神帮写的膜拜一下)的相关文章

mongodb group操作 以及管道 aggregate 分组排序分页

分组获取数据: db.express_info.group({ "key":{"express_code":true}, "initial":{"num":"0","mobile":"0"}, "reduce":function(doc,result){result.num++, result.mobile=doc.mobile}, "c

【JavaWeb】参考大神重新写的上传下载

自学java,选择从简单的javaweb入手了. 读了网上某位大神的细致文章,了解后自己写了这篇上传下载的,加上了自己一部分的理解注释. 算是入门篇吧.大致完成了任务,比较粗糙. fileHanderServlet.java (文件上传处理) 1 package up.file.Servletfile; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 i

求大神帮我解决下webuploader另外添加参数的问题,我知道是formData添加额外参数给后台

如图所示: 我点击开始上传的时候.会有3个请求,等于是他会返回我3次action,我在后台添加了额外的2个参数dkName,Moneys. 求大神可以帮我看看如何改写下:我需要他每一个请求只会带上图下当前的点卡名称和点卡金额,如果有大神大牛乐意帮助下我,可以加我QQ79612086或者在下面回复,我会及时收看,谢谢!!

ssh整合案例出了could not initialize proxy - no Session 问题,那个大神帮我看看

在做SSH项目时,控制台出现这个错误,请大神帮忙看看啊2017-04-14 11:15:57,902 DEBUG (org.springframework.beans.factory.support.DefaultListableBeanFactory:1140) - Added autowiring by name from bean name 'cn.bdqn.jboa.action.EmployeeAction' via property 'employeeBiz' to bean nam

求大神帮解答javaEE这个问题,谢谢了

List<Privilege> privilegeList = privilegeService.findAll(); ActionContext.getContext().put("privilegeList", privilegeList); <s:checkboxlist name="privilegeIds" list="#privilegeList" listKey="id" listValue=&

java自定义ArrayList排序, 大神略过

一个list里面有两种对象,现在有个需求必须对不同的对象的日期来排序,以下是代码 /** *@author 肖华 [email protected] */ List<Message> messages=messageSrv.getList(u.getUserId()); List<Object> messageList=new ArrayList<Object>(); for(Object o:messages){ messageList.add(o); } for(O

在下新手!练习shell脚本遇到一个问题请大神帮帮忙!

脚本信息: #!/bin/bash # MIAO=`history | tail -1 | cut -d' ' -f2` if        [ $MIAO -gt 1000 ]; then        echo "Some command will gone." else        echo "OK." fi   错误信息: [[email protected] practice]# ./text8.sh ./text8.sh: line 5: [: -gt

VS 中的一个BUG?求大神帮我解答下。

首先,希望知道这个问题的人能够帮我解答下. 来看代码: int main() { long long int result=-321; cout<<result<<endl; if(INT_MIN==-2147483648) cout<<"相等"<<endl; if( result<-2147483648) cout<<"1"<<endl; if( result<INT_MIN) c

求大神帮我看看我的程序哪里错了

2owlfz勤沽斯眉郊瘸<http://weibo.com/FDuoEQyp/230927983067401178320896> l6uo43信谈炎缘猛睬<http://weibo.com/p20180413Pp/230927982988153436512256> 2k88f0蛋诜渭郴耗寄<http://weibo.com/Vxnyap/230927983155096823013376> j0iv8a炊毒菩褪胤布<http://weibo.com/20180413