SQL Select N to M Records (single Table)
取表里n到m条纪录的几种方法:
1. 只需要查询前M条数据(0 to M),
1.1 使用 top(M) 方法:
select top(3) * from [tablename]
1.2 使用 set rowcount 方法:
http://msdn.microsoft.com/zh-cn/library/ms188774(SQL.90).aspx
set rowcount M
select * from [tablename]
set rowcount 0
权限 要求具有 public 角色成员资格。
要执行set rowcount 0, 否则影响以后查询等.
2.查询N到M条数据(N to M),
2.1 表里面有标识列
2.1.1
select top (M-N+1) * from [tablename] where [columnname] not in (select top (N) [columnname] from [tablename])
2.1.2 逆序显示
select top N * from (select top M * from [tablename] order by [columnname]) temp order by [columnname] desc
2.1.3 顺序显示
select * from (select top N * from (select top M * from [tablename] order by [columnname]) temp1 order by [columnname] desc) temp2 order by [columnname]
2.2 表里有identity属性
select * from [tablename] where identitycol between N and M
如[columnname]为identity属性,则可以写成:
select * from [tablename] where [columnname] between N and M
2.3 表里面有标识列, 利用临时表
IF Exists(Select 1 from sysObjects Where Name ='temptable' And Type In ('temptable','U'))
begin
drop table [temptable]
end
select top M * into [temptable] from [tablename] order by [columnname]
set rowcount N
select * from [temptable] order by [columnname] desc
set rowcount 0
drop table [temptable]
2.4 如果tablename里没有其他identity列,那么:
exec sp_dboption [DataBaseName] ,'select into/bulkcopy',true
IF Exists(Select 1 from sysObjects Where Name ='temptable' And Type In ('temptable','U'))
begin
drop table temptable
end
select identity(int) id0,* into [temptable] from [tablename]
select * from temp where id0 >= N and id0 <= M
drop table temptable
如果你在执行select identity(int) id0,* into [temp
相关文档:
Tree表如下:
NodeId ParentId NodeName
0 -1 &nb ......
要求: 按 lct1, lct2 排序后的前两条纪录,显示为下列结果:
item_cd1 item_cd2 lct
01 a   ......
从官网下载SQL Server 2008的180天试用版其实与正式版内容是基本相同的,唯一的区别就在于安装配置文件中所包含的key。各种版本的SQL Server在进行到这一步之前都是完全一样的:
Microsoft® SQL Server® 2008 Enterprise Evaluation:开发人员试用体验
Microsoft® SQL Server® 2008 Enterprise Evaluatio ......
sql group by 用法
2009-07-16 11:01:00 业界 | 评论(0) | 浏览(1676)
group by主要是用来分组的,怎么个分组呢?
以下用两个例子说明两个使用方面,1是合理的返回合计值(防止笛卡尔积现象),2是用分组来找出重复的记录
============================================================== ......
1,SELECT *,CASE OrderStatus WHEN 1 THEN '未处理' when 2 THEN '锁定' when
3 THEN '已出票' ELSE '过期' END
from dbo.T_OrderItem
2,
SELECT *,CAST(ROUND(CAST (HostWin AS FLOAT)/(HostWin+HostDraw+HostBear),2)*100 AS varchar)+'%' AS HostWinRate,
& ......