SQL循环索引
declare @tbl table(order_id varchar(20),dates datetime)
insert @tbl(order_id,dates)
select 'A','2014-1-1' union
select 'A','2014-2-1' union
select 'A','2014-2-2' union
select 'B','2014-1-1' union
select 'C','2014-1-1' union
select 'D','2014-1-1' union
select 'D','2014-1-2' union
select 'E','2014-12-1' union
select 'E','2014-12-2' union
select 'E','2014-12-3' declare @index int,@count int,@row int,@item_order varchar(20),@current_order varchar(20) select identity(int,1,1) as id,order_id,dates,cast(0 as int) as row_index into #tbl from @tbl order by order_id,dates
select @index=1,@row=1,@count=@@rowcount while @row<=@count
begin
--首次循环
if isnull(@item_order,'')=''
select @item_order=order_id from #tbl where id=@row
--已经有做循环
else
begin
select @current_order=order_id from #tbl where id=@row
if @current_order<>@item_order
begin
set @index=1
set @item_order=@current_order
end
end
--更新序列号
update #tbl set row_index=@index where id=@row
set @index=@index+1
set @row=@row+1
end
select * from #tbl
drop table #tbl
SQL循环索引的更多相关文章
- SQL Server索引设计 <第五篇>
SQL Server索引的设计主要考虑因素如下: 检查WHERE条件和连接条件列: 使用窄索引: 检查列的选择性: 检查列的数据类型: 考虑列顺序: 考虑索引类型(聚集索引OR非聚集索引): 一.检查 ...
- SQL Server 索引维护(1)——系统常见的索引问题
前言: 在很多系统中,比如本人目前管理的数据库,索引经常被滥用,甚至使用DTA(数据库引擎优化顾问)来成批创建索引(DTA目前个人认为它的真正用处应该是在发现缺失的统计信息,在以前的项目中,用过一次D ...
- SQL Server 索引维护:系统常见的索引问题
在很多系统中,比如本人目前管理的数据库,索引经常被滥用,甚至使用DTA(数据库引擎优化顾问)来成批创建索引(DTA目前个人认为它的真正用处应该是在发现缺失的统计信息,在以前的项目中,用过一次DTA,里 ...
- SQL Server索引总结二
从CREATE开始 通过显式的CREATE INDEX命令 在创建约束时作为隐含的对象 随约束创建的隐含索引 当向表中添加如下两种约束之一时,就会创建隐含索引. 主键约束(聚集索引) 唯一约束(唯一索 ...
- 【译】SQL Server索引进阶第八篇:唯一索引
原文:[译]SQL Server索引进阶第八篇:唯一索引 索引设计是数据库设计中比较重要的一个环节,对数据库的性能其中至关重要的作用,但是索引的设计却又不是那么容易的事情,性能也不是那么轻易就 ...
- SQL Server2005索引碎片分析和解决方法
SQL Server2005索引碎片分析和解决方法 本文作者(郑贤娴),请您在阅读本文时尊重作者版权. 摘要: SQL Server,为了反应数据的更新,需要维护表上的索引,因而这些索引会形成碎片.根 ...
- 转: SQL Server索引的维护 - 索引碎片、填充因子
转:http://www.cnblogs.com/kissdodog/archive/2013/06/14/3135412.html 实际上,索引的维护主要包括以下两个方面: 页拆分 碎片 这两个问题 ...
- SQL Server索引 (原理、存储)聚集索引、非聚集索引、堆 <第一篇>
一.存储结构 在SQL Server中,有许多不同的可用排列规则选项. 二进制:按字符的数字表示形式排序(ASCII码中,用数字32表示空格,用68表示字母"D").因为所有内容都 ...
- SQL 创建索引的作用以及如何创建索引
SQL 创建索引的作用以及如何创建索引 SQL 创建索引的作用 一.使用索引的优点: 1.通过唯一性索引(unique)可确保数据的唯一性 2.加快数据的检索速度 3.加快表之间的连接 4.减少分组和 ...
随机推荐
- token原理
token原理1.和session有很大关系哦. jsp生成表单时,在表单中插入一个隐藏<input>字段,该字段就是保存在页面端的token字符串,同时把该字符串存入session中.等 ...
- Thinking in Java——笔记(5)
Initialization & Cleanup Guaranteed initialization with the constructor In Java, the class desig ...
- Thinking in Java--笔记(2)
Everything Is an Object You manipulate objects with references Each programming language has its own ...
- P1967 货车运输 -60分
打了一个最大生成树+dfs,60分成功tle #include <bits/stdc++.h> using namespace std; const int maxn = 10005; c ...
- php安装memcache注意事项
有没有谁遇到过这样的问题:在php.ini里面 extension=php_memcache.dll添加了memcache扩展,在ext目录里也添加了php_memcache.dll文件.但用phpi ...
- 获取唯一UUID/UDID方案
概述 如何保证获取到的UUID能够唯一标识每一台设备呢?我们知道通过UIDevice可以获取到UUIDString,但是如果App被删除了然后重新安装,就会得到不同的UUIDString,这并不是我们 ...
- butterknife异常提示:attribute value must be constant
就是因为你的android工程是lib类型的 如: apply plugin: 'com.android.library' android { compileSdkVersion 23 buildTo ...
- Convert Sorted Array to Binary Search Tree || LeetCode
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * s ...
- rndc控制远程dns服务器配置方法
1- 如果不存在/etc/rndc.conf touch /etc/rndc.conf chown named:named /etc/rndc.conf 2- rndc-confgen > /e ...
- Django,数据模型创建之数据库API参考(转载)
一旦 数据模型 创建完毕, 自然会有存取数据的需要.本文档介绍了由 models 衍生而来的数据库抽象API,及如何创建,得到及更新对象. 贯穿本参考, 我们都会引用下面的民意测验(Poll)应用程序 ...