易截截图软件、单文件、免安装、纯绿色、仅160KB

求一sql语句 - MS-SQL Server / 基础类


id bh  
1 10
2 11
3 12 
4 15
5 16
6 22
7 25
8 26
9 27
10 28

将bh按连续分段出来返回字符串:
10~12,15~16,22,25~28
SQL code:




declare @t table(num int)
insert into @t select 1
union all select 2
union all select 3
union all select 4
union all select 5
union all select 12
union all select 17
union all select 18
union all select 19
union all select 20
union all select 25

select
rtrim(a.num)+(case when min(b.num)!=a.num then '-'+rtrim(min(b.num)) else '' end)
from
(select t.num from @t t where not exists(select 1 from @t where num=t.num-1)) a,
(select t.num from @t t where not exists(select 1 from @t where num=t.num+1)) b
where
a.num<=b.num
group by a.num

-------------------------
1-5
12
17-20
25
*/

{{


相关问答:

Asp+sql server问题 - Web 开发 / ASP

我一个项目,有个插入操作,具体是这样的:
我有进货信息表。在出货时选择相应的进货信息,输入数量,选择部门后,点保存按钮,由于网络延时,点一下没有反映,于是用户就又点一下,导致一次插入了两条记录:
例: ......

sql性能求助 - MS-SQL Server / 疑难问题

场景如下:
客户把备份好的数据库,发给我,我在本机还原后,运行写好的存储过程,比较快,并且在实施那边运行同样比较快。但是当实施在客户那边运行的时候速度就非常的慢,时间超出了程序的时间限制。远程在客户那 ......

求一条SQL - MS-SQL Server / 疑难问题

--drop table #T1
--drop table #T2
create Table #T1(ID int,
QueryID nvarchar(20),
ResultID1 nvarchar(20),
ResultID2 nvarchar(20))
create Table #T2(SortNo int,
QueryID nvarchar(20),
ResultID1 nv ......

SQL如何优化问题 - MS-SQL Server / 疑难问题

今天做了一个存储过程   环境是SQL2000数据库  
大致如下
建立临时表
定义员工游标
        循环员工(属于1个公司)  
        ......

SQL clr - MS-SQL Server / 非技术版

想用SQL CLR做如下的存储过程,但是发现报错: 
{由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值。} 
而且如果数据从case 1通过时就是正确的,从case 2通过时错误 
并且在c ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号