问题是这样的:
我有一张表 表结构(部门,单证代码,单证号)
本来是清单形式,可是需求部门希望能够将同一部门同一单证代码的统一起来
即(部门,单证代码,单证起始号,单证终止号,份数)
我给如何处理好呢?
注:单证号中间有可以有断层的情况,只有连续的号码才能统计在一起。
及如 1 2 3 5 6 8 9
只能是 单证起始号 单证终止号 份数
1 3 3
5 6 2
8 9 2
谢谢。
我有一张表 表结构(部门,单证代码,单证号)
本来是清单形式,可是需求部门希望能够将同一部门同一单证代码的统一起来
即(部门,单证代码,单证起始号,单证终止号,份数)
我给如何处理好呢?
注:单证号中间有可以有断层的情况,只有连续的号码才能统计在一起。
及如 1 2 3 5 6 8 9
只能是 单证起始号 单证终止号 份数
1 3 3
5 6 2
8 9 2
谢谢。
解决方案 »
- 关于数据库设计 字典表结构 很困惑?
- 求一个简单的sql
- 数据库列间判断输出问题
- 求一SQL语句
- 求一条比较简单的insert 语句
- 现在新建一个数据库,可以直接把以前的一个sql数据库文件恢复进去吗?
- sql sever 2005安装遇到的问题,哪位大哥帮忙看一下,分不够,可以加!!!!!!!!!!!!!!!!!
- SQL2005发布订阅问题!
- 我想把硬盘上的图片文件(比如:d:\pic\1.jpg)写入一个表的image字段中,应该如何写sql语句?
- 我想在delphi中用线程(TThread)进行sql server连接,目的是为了避免在连接数据库时应用程序在一段时间内失去焦点?
- 使用储存过程会被侵入吗
- 08中 cross apply报错('.' 附近有语法错误。)
from tb
group by
select MIN(col1) as startnum,
grp as endnum
from(
select col1,
(select MIN(col1) from T1 a where a.col1>=k.col1 and
not exists(select * from T1 where a.col1+1=col1 )) as grp
from T1 k ) z
group by grp
--方法2:利用ROW_NUMBER (sql2005)
select MIN(col1) as startnum,
max(col1) as endnum
from(
select col1,
col1-ROW_NUMBER()over(order by col1) as grp
from T1 k ) z
group by grp
/*
startnum endnum
----------- -----------
1 3
100 101
103 106*/
if exists(select [name] from tempdb.dbo.sysobjects where id = object_id('tempdb..#temp1'))
drop table #temp1select 1 as fldSeq into #temp1 union
select 2 union
select 3 union
select 5 union
select 6 union
select 8 union
select 9 select
MIN(fldSeq) as startnum,
grp as endnum,
grp - MIN(fldSeq) + 1 as Qty
from(
select fldSeq,
(select MIN(fldSeq) from #temp1 a where a.fldSeq>=k.fldSeq and
not exists(select * from #temp1 where a.fldSeq+1=fldSeq )) as grp
from #temp1 k ) z
group by grp/*
startnum endnum Qty
----------- ----------- -----------
1 3 3
5 6 2
8 9 2(所影响的行数为 3 行)
*/