计算连续号处理方式.
2005-11-30 19:53
267 查看
原贴:http://community.csdn.net/Expert/topic/4428/4428867.xml?temp=.4289362
id 批次
10001 1
10002 1
10003 1
。。。 1
10011 1
20001 2
20002 2
20003 3
想得到的结果
id 批次
10001-11 1
20001-2 2
20003 3
就是说只要是一批商品,他的id都是连号,如果此批不止一条就合并末尾数不一样的中间用一个“-”符号,每一批的数量在另一个表中有值
--更正显示结果.
declare @t table(id varchar(10),批次 int)
insert into @t select '10001',1
union all select '10002',1
union all select '10003',1
union all select '10004',1
union all select '10005',1
union all select '10006',1
union all select '10007',1
union all select '10008',1
union all select '10009',1
union all select '10010',1
union all select '10011',1
union all select '20001',2
union all select '20002',2
union all select '20003',3
--查询处理
select id=case when b.id=b.id1 then b.id
else b.id+'-'+cast(convert(int,b.id1)-convert(int,b.id)+1 as varchar(10)) end,
批次=a.批次
from @t a,(
select id,id1=(select min(id) from @t a1
where id>=a.id
and not exists(
select * from @t
where 批次=a1.批次 and id=a1.id+1))
from @t a
where not exists(
select * from @t
where 批次=a.批次 and id=a.id-1))b
where a.id between b.id and b.id1
group by
b.id,b.id1,a.批次
--结果
id 批次
--------------------- -----------
10001-11 1
20001-2 2
20003 3
(所影响的行数为 3 行)
id 批次
10001 1
10002 1
10003 1
。。。 1
10011 1
20001 2
20002 2
20003 3
想得到的结果
id 批次
10001-11 1
20001-2 2
20003 3
就是说只要是一批商品,他的id都是连号,如果此批不止一条就合并末尾数不一样的中间用一个“-”符号,每一批的数量在另一个表中有值
--更正显示结果.
declare @t table(id varchar(10),批次 int)
insert into @t select '10001',1
union all select '10002',1
union all select '10003',1
union all select '10004',1
union all select '10005',1
union all select '10006',1
union all select '10007',1
union all select '10008',1
union all select '10009',1
union all select '10010',1
union all select '10011',1
union all select '20001',2
union all select '20002',2
union all select '20003',3
--查询处理
select id=case when b.id=b.id1 then b.id
else b.id+'-'+cast(convert(int,b.id1)-convert(int,b.id)+1 as varchar(10)) end,
批次=a.批次
from @t a,(
select id,id1=(select min(id) from @t a1
where id>=a.id
and not exists(
select * from @t
where 批次=a1.批次 and id=a1.id+1))
from @t a
where not exists(
select * from @t
where 批次=a.批次 and id=a.id-1))b
where a.id between b.id and b.id1
group by
b.id,b.id1,a.批次
--结果
id 批次
--------------------- -----------
10001-11 1
20001-2 2
20003 3
(所影响的行数为 3 行)
相关文章推荐
- 计算连续号处理方式10001-11
- delphi 对int64计算的一种处理方式
- [技术无关] 以连续的方式对外部数据进行储存、处理的所有物体,都可称作一个“活”的物体
- A、B两伙马贼意外地在一片沙漠中发现了一处金矿,双方都想独占金矿,但各自的实力都不足以吞下对方,经过谈判后,双方同意用一个公平的方式来处理这片金矿。处理的规则如下:他们把整个金矿分成n段,由A、B开始轮流从最左端或最右端占据一段,直到分完为止。 马贼A想提前知道他们能分到多少金子,因此请你帮忙计算他们最后各自拥有多少金子?(两伙马贼均会采取对己方有利的策略)
- 百练+打印月历+日期的计算和格式化输出方式,细节注意处理额
- 对连续数值进行指定方式离散化,计算分布,用cut函数
- XMPP 连续注册出现用户限制:500错误 处理方式
- python中一种完全可能情况带入计算的处理方式
- datagrid的文字换行与连续字符串换行处理,字符串三种截取方式
- 用最高效的方式计算2*8
- Math:处理数学计算的工具
- 【总结】Ajax Asp.Net 交互处理方式(三)
- 以全局监听的方式处理img的error事件
- Java事件处理机制-事件监听器的四种实现方式
- zzuli 1913: 小火山的计算能力(简单字符串处理 + 加减求和)
- linux服务器每秒并发处理数的计算方法
- Struts2一个Action内包含多个请求处理方法(三种方式)
- jquery radio设置checked但按钮radio仍然没选中的js处理方式
- SparkGraph 与SparkDataFrame 两种方式计算朋友的二度关系
- 模组和镜头配套后焦距的计算方式