【小小问题集锦13之 关于分组加顺序号的SQL写法】
/*
原表:
thid other
a 1
a 1
b 0
b 0
b 0
c 2
c 2
希望变成:
thid other
a 1
a 2
b 0
b 1
b 2
c 2
c 3
*/
if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (id varchar(10) ,value int)
insert tb select
'a' , 1 union all select
'a' , 1 union all select
'b' , 0 union all select
'b' , 0 union all select
'b' , 0 union all select
'c' , 2 union all select
'c' , &nb
相关文档:
应一个朋友的要求,贴上收藏的SQL常用分页的办法~~
表中主键必须为标识列,[ID] int IDENTITY (1,1)
1.分页方案一:(利用Not In和SELECT TOP分页)
语句形式:
SELECT TOP 页记录数量 *
from 表名
WHERE (ID NOT IN
(SELECT TOP (每页行数*(页数-1)) ID
from 表名
ORDER BY ......
SQL Server 得到行号的SQL
使用临时表:
select id=identity(int,1,1),value into #temp from YourTable
select * from #temp
drop table #temp
取得第11到20行记录:
select IDENTITY(in ......
有时在做程序时,测试时的数据,要拿像用户演示,数据库的附加是最直接的办法,但若从SQL Server 2005转向2000导出麻烦,内容也多,生成SQL语句是最好的办法,也是在网上找的工具,mssql2导出非常方便,担心我以后难得找所以放在这里,以备后用。 ......
MS SQL Server2000 数据源配置
(注:sqljdbc.jar下载不到的话,找我索取pengqinghui110@126.com)
1、在项目的WebRoot下的META-INF中新建context.xml文件。内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/sqlserver"
&nbs ......
1 用UNION替换OR (适用于索引列)
通常情况下, 用UNION替换WHERE子句中的OR将会起到较好的效果. 对索引列使用OR将造成全表扫描. 注意, 以上规则只针对多个索引列有效.
如果有column没有被索引, 查询效率可能会因为你没有选择OR而降低. 在下面的例子中, LOC_ID 和REGION上都建有索引.
高效: SELECT LOC_ID , LOC_DESC , ......