group_by分页(Sql Server)
作者:敖士伟
一张有group by后可能很多重复行,这时用not in等基于唯一列的分布算法会存在问题。
我的解决办法是:
一张表有一个id int的主键,对其它列进行group by,分页思想是:把max(id)做group by后的唯一列,还是用not in的分布思想。
例:
select top 4 sum(int_TZ2_id) as id,dt_TZ2_date,vchar_TZ2_PingZheng,vchar_TZ2_ZaiYao,vchar_TZ2_DanSN,convert(decimal(18,4),sum(float_TZ2_J_ShuLiang)) as j_sl, convert(decimal(18,4),sum(money_TZ2_J_JingE)) as j_je,convert(decimal(18,4),sum(float_TZ2_D_ShuLiang)) as d_sl, convert(decimal(18,4),sum(money_TZ2_D_JingE)) as d_je from tbTaiZhang2
where 1=1 and 2=2 group by dt_TZ2_date,vchar_TZ2_PingZheng,vchar_TZ2_ZaiYao,vchar_TZ2_DanSN
having (max(int_TZ2_id) NOT IN
(SELECT TOP 4 max(int_TZ2_id) as id
from tbTaiZhang2
WHERE 1=1 and 2=2 group by dt_TZ2_date,vchar_TZ2_PingZheng,vchar_TZ2_ZaiYao,vchar_TZ2_DanSN
ORDER BY dt_TZ2_date,id ASC))
ORDER BY dt_TZ2_date,id ASC
相关文档:
create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',
@pagesize int output,--每页显示记录条数
@currentpage int output,--第几页
@orderid nvarchar(50),--主键排序
@sort int,--排序方式,1表示升序,0表示降序排列
......
系统环境:Windows 7
软件环境:Visual C++ 2008 SP1 +SQL Server 2005
本次目的:编写一个航空管理系统
这是数据库课程设计的成果,虽然成绩不佳,但是作为我用VC++ 以来编写的最大程序还是传到网上,以供参考。用VC++ 做数据库设计并不容易,但也不是不可能。以下是我的程序界面,后面 ......
背景:
sql select top N 语句是一个非常重要的语句, 在实现分页查询中是不可或缺的. 由于分页查询通常涉及含有大量记录的表, 并且是为大量用户分享的任务,因此,对其进行优化是很有意义的。
实现sql top N 的功能有几种变种:
1. set rowcount @n; select ... order by somefields
2. select top (@n) .... order by ......
package cn.com.hbivt.util;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class StringUtils {
//过滤通过页面表单提交 ......
在网上下了一个版本的SQL2008,一步步安装,安装过程中遇到了redist.cab 和Sql.cab错误,但基本功能还是能用,但还是不能容忍错误的存在,经一番搜索,终于找到了解决方案:
下载msxml安装,我一口气安装了msxml4.0 sp3和msxml6.0两个文件
为什么安装这个东西那?
想起来了,因为安装时报的错误是与网络有关系,而我的win ......