按日期范围查询ID断号情况,并进行聚合求和
来源:互联网 发布:淘宝保证金1000能退吗 编辑:程序博客网 时间:2024/06/04 18:23
问:
有一个表如下:
ID saleDate Price
1 2008-1-1 40
2 2008-1-1 50
3 2008-1-2 24
6 2008-1-2 10
7 2008-1-3 12
8 2008-1-3 20
....
要写一个存储过程,统计一段时间内各连续ID的价格合计,得到以下结果:
比如 2008-1-1到2008-1-3,得到的结果为:
ID_Range Totoal_Price
1-3 114
6-7 42
如果统计的是2008-1-1到2008-1-2,得到的结果是
ID_Range Totoal_Price
1-3 114
6 10
请问如何写这个存储过程?
答:
if object_id('tempdb..#T') is not null
drop table #T
create table #T(ID int,SaleDate datetime,Price int)
insert into #T select 1 , '2008-1-1' , 40
insert into #T select 2 , '2008-1-1' , 50
insert into #T select 3 , '2008-1-2' , 24
insert into #T select 6 , '2008-1-2' , 10
insert into #T select 7 , '2008-1-3' , 12
insert into #T select 8 , '2008-1-3' , 20
go
create proc p_test
(
@begin_date datetime,
@end_date datetime
)
as
select ID=ltrim(ID)+
case when exists(select 1 from #T
where SaleDate between @begin_date and @end_date
and ID=a.ID+1) then '-'+
ltrim((select min(ID) from #T b
where SaleDate between @begin_date and @end_date and ID>=a.ID
and not exists(select 1 from #T
where SaleDate between @begin_date and @end_date
and ID=b.ID+1)
)) else '' end ,
Total_Price=(select sum(Price) from #T b
where SaleDate between @begin_date and @end_date
and ID between a.ID and
(select min(ID) from #T b
where SaleDate between @begin_date and @end_date and ID>=a.ID
and not exists(select 1 from #T
where SaleDate between @begin_date and @end_date
and ID=b.ID+1)
)
)
from #T a
where SaleDate between @begin_date and @end_date
and not exists
(select 1 from #T where ID=a.ID-1 and SaleDate between @begin_date and @end_date)
go
exec p_test '2008-01-01','2008-01-03'
exec p_test '2008-01-01','2008-01-02'
go
drop table #T
drop proc p_test
/**//*
ID Total_Price
------------------------- -----------
1-3 114
6-8 42
(所影响的行数为 2 行)
ID Total_Price
------------------------- -----------
1-3 114
6 10
(所影响的行数为 2 行)
*/
drop table #T
create table #T(ID int,SaleDate datetime,Price int)
insert into #T select 1 , '2008-1-1' , 40
insert into #T select 2 , '2008-1-1' , 50
insert into #T select 3 , '2008-1-2' , 24
insert into #T select 6 , '2008-1-2' , 10
insert into #T select 7 , '2008-1-3' , 12
insert into #T select 8 , '2008-1-3' , 20
go
create proc p_test
(
@begin_date datetime,
@end_date datetime
)
as
select ID=ltrim(ID)+
case when exists(select 1 from #T
where SaleDate between @begin_date and @end_date
and ID=a.ID+1) then '-'+
ltrim((select min(ID) from #T b
where SaleDate between @begin_date and @end_date and ID>=a.ID
and not exists(select 1 from #T
where SaleDate between @begin_date and @end_date
and ID=b.ID+1)
)) else '' end ,
Total_Price=(select sum(Price) from #T b
where SaleDate between @begin_date and @end_date
and ID between a.ID and
(select min(ID) from #T b
where SaleDate between @begin_date and @end_date and ID>=a.ID
and not exists(select 1 from #T
where SaleDate between @begin_date and @end_date
and ID=b.ID+1)
)
)
from #T a
where SaleDate between @begin_date and @end_date
and not exists
(select 1 from #T where ID=a.ID-1 and SaleDate between @begin_date and @end_date)
go
exec p_test '2008-01-01','2008-01-03'
exec p_test '2008-01-01','2008-01-02'
go
drop table #T
drop proc p_test
/**//*
ID Total_Price
------------------------- -----------
1-3 114
6-8 42
(所影响的行数为 2 行)
ID Total_Price
------------------------- -----------
1-3 114
6 10
(所影响的行数为 2 行)
*/
- 按日期范围查询ID断号情况,并进行聚合求和
- 按日期查询 并求和
- postgresql 按日期范围查询
- postgresql 按日期范围查询
- MongoDB 日期查询与mongodump 按日期范围导出数据
- Oracle按日期累计求和
- 关于按日期查询
- 按日期查询记录
- MYSQL按日期查询
- 按日期查询
- 补全日期范围并查询
- MySql按日期进行统计
- sql 按日期分组查询
- SQL按日期来查询
- 数查询按日期分组
- ADO.Net 按日期查询
- Linq按日期统计查询
- oracle 按id统计出金额的总数,并按日期显示出最新的数据
- 80后的我们
- 抛砖引玉-使用Acegi实现多种用户登录的一种方案
- 程序员之路-学习心得
- ibatis变量的小差异#和$
- yinyong
- 按日期范围查询ID断号情况,并进行聚合求和
- 堆和栈的区别
- 思考:Portal是应用吗?
- 内联函数和普通函数的区别
- Tuxedo配置管理简要说明
- 开始→运行→输入的命令集锦
- 一点一滴学习IT日语2
- Tuxedo配置及简单应用
- GridView中DropDownList的事件