MSSQL求連續ID內數量合計-常見解決方案
来源:互联网 发布:java参数传递构造函数 编辑:程序博客网 时间:2024/04/26 12:30
--> Title : MSSQL求連續ID內數量合計-常見解決方案
--> Author : wufeng4552
--> Date : 2009-12-04
if object_id('tb') is not null drop table tb
go
create table tb(ID varchar(10),Num decimal(10,2))
insert tb
select '001',200.00 union all
select '002',200.00 union all
select '003',300.00 union all
select '007',500.00 union all
select '008',800.00 union all
select '009',200.00 union all
select '012',100.00 union all
select '013',100.00 union all
select '014',200.00 union all
select '017',100.00 union all
select '018',400.00 union all
select '019',300.00
-->方法 1 臨時表
if object_id('tempdb..#t1')is not null
drop table #t1
if object_id('tempdb..#t2')is not null
drop table #t2
go
--取出起號
select cnt=identity(int,1,1),* into #t1 from tb t where not exists(select 1 from tb where id=t.id-1)order by t.id
--取出止號
select cnt=identity(int,1,1),* into #t2 from tb t where not exists(select 1 from tb where id=t.id+1)order by t.id
select n.[start]+'-'+n.[end]起止號,sum(num)合計
from tb m,
(select a.ID [start],b.ID [end] from #t1 a,#t2 b where a.cnt=b.cnt) n
where m.ID between n.[start] and n.[end]
group by n.[start]+'-'+n.[end]
/*
起止號 合計
--------------------- www.52mvc.com---------------------------------------
001-003 700.00
007-009 1500.00
012-014 400.00
017-019 800.00
(4 個資料列受到影響)
*/
--方法 2
select case when min(t.id)!=max(t.id) then min(t.id)+'-'+max(t.id)else min(t.id)end 起止號,
sum(num)合計
from(
select ID,
cnt=cast(ID as int)-(select count(*)from tb n where m.ID>n.ID),
num
from tb m
)t group by cnt
/*
起止號 合計
--------------------- ---------------------------------------
001-003 700.00
007-009 1500.00
012-014 400.00
017-019 800.00
(4 個資料列受到影響)
*/
--方法3
select case when min(t.id)!=max(t.id) then min(t.id)+'-'+max(t.id)else min(t.id)end 起止號,
sum(num)合計
from(
select id,cnt=id-row_number()over(order by getdate()),num from tb
)t group by cnt
/*
起止號 合計
--------------------- ---------------------------------------
001-003 700.00
007-009 1500.00
012-014 400.00
017-019 800.00
(4 個資料列受到影響)
*/
--> Author : wufeng4552
--> Date : 2009-12-04
if object_id('tb') is not null drop table tb
go
create table tb(ID varchar(10),Num decimal(10,2))
insert tb
select '001',200.00 union all
select '002',200.00 union all
select '003',300.00 union all
select '007',500.00 union all
select '008',800.00 union all
select '009',200.00 union all
select '012',100.00 union all
select '013',100.00 union all
select '014',200.00 union all
select '017',100.00 union all
select '018',400.00 union all
select '019',300.00
-->方法 1 臨時表
if object_id('tempdb..#t1')is not null
drop table #t1
if object_id('tempdb..#t2')is not null
drop table #t2
go
--取出起號
select cnt=identity(int,1,1),* into #t1 from tb t where not exists(select 1 from tb where id=t.id-1)order by t.id
--取出止號
select cnt=identity(int,1,1),* into #t2 from tb t where not exists(select 1 from tb where id=t.id+1)order by t.id
select n.[start]+'-'+n.[end]起止號,sum(num)合計
from tb m,
(select a.ID [start],b.ID [end] from #t1 a,#t2 b where a.cnt=b.cnt) n
where m.ID between n.[start] and n.[end]
group by n.[start]+'-'+n.[end]
/*
起止號 合計
--------------------- www.52mvc.com---------------------------------------
001-003 700.00
007-009 1500.00
012-014 400.00
017-019 800.00
(4 個資料列受到影響)
*/
--方法 2
select case when min(t.id)!=max(t.id) then min(t.id)+'-'+max(t.id)else min(t.id)end 起止號,
sum(num)合計
from(
select ID,
cnt=cast(ID as int)-(select count(*)from tb n where m.ID>n.ID),
num
from tb m
)t group by cnt
/*
起止號 合計
--------------------- ---------------------------------------
001-003 700.00
007-009 1500.00
012-014 400.00
017-019 800.00
(4 個資料列受到影響)
*/
--方法3
select case when min(t.id)!=max(t.id) then min(t.id)+'-'+max(t.id)else min(t.id)end 起止號,
sum(num)合計
from(
select id,cnt=id-row_number()over(order by getdate()),num from tb
)t group by cnt
/*
起止號 合計
--------------------- ---------------------------------------
001-003 700.00
007-009 1500.00
012-014 400.00
017-019 800.00
(4 個資料列受到影響)
*/
- MSSQL求連續ID內數量合計-常見解決方案
- MSSQL求連續ID內數量合計-常見解決方案
- MSSQL求連續ID內數量合計-常見解決方案
- MSSQL求連續ID內數量合計-常見解決方案
- MSSQL編號重排常見解決方案
- MSSQL求中位數-常見解決方案-整理帖
- MSSQL編號重排常見解決方案
- MSSQL求中位數-常見解決方案-整理帖
- MSSQL动态分区方案
- MSSQL根据ID进行分页
- MSSQL計算表達式的值常見解決方案
- MSSQL計算表達式的值常見解決方案
- 分布式ID生成方案
- ID生成方案
- [MSSQL]根据身份证ID获取户籍所在地
- MSSQL 获取无限级表中父节点ID
- iOS设备ID替代方案
- iOS 设备ID 替代方案
- 《新编数据结构算法考研指导 》的说明
- 【hdoj2570】迷瘴
- 【hdoj2734】Quicksum
- 【hdoj1863】畅通工程
- 323. Nothing in the world is difficult for one who sets his mind to it. 世上无难事,只怕有心人
- MSSQL求連續ID內數量合計-常見解決方案
- 静态SQL与动态SQL的比较
- 对字符串常量的修改
- java中代码检查checkStyle结果分析 .
- static和extern修饰连接属性
- static的作用
- C库文件说明
- Lustre I/O性能特点
- hdu 4339 不错的线段树