首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视 ┆淘宝手机在线充值 ┆淘宝游戏点卡充值 
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>数据库>SQL数据库>sql server存储过程实例:统计一段时间内各连续ID的价格合计
sql server存储过程实例:统计一段时间内各连续ID的价格合计
来源: 发布时间:2008-04-22 发布人: 浏览: 人次   字体: [ ]  

问:
有一个表如下:
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+1then '-'+
               
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 行)
*/


相 关 文 章   发布商链接
·SQLServer2005分解导入xml文件代码
·用sql实现查询重复记录的代码
·SQLServer常用日期格式转换方法及其...
·SqlServr利用触发器来控制某列不让用...
·sql删除数据库中的所有表语句
·如何手工启动SQLServer的全文索引?
·SQL Server2005中删除表中重复行的方...
·SQLServer2005导入xml文件实例代码
·ISNUMERIC函数介绍及其要注意的地方
·sql语句获得数据库某表字段的数据类...
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·SQL数据库实现用SQL语句根据...
·SqlServer 2005 Express的配...
·sql排序规则的简介及其修改操
·如何用sql语句为字段添加和去
·sqlserver数据库日志文件压缩
·sql语句获得数据库某表字段的
·ISNUMERIC函数介绍及其要注意
·SQLServer2005导入xml文件实...
·SQL Server2005中删除表中重...
·如何手工启动SQLServer的全文
·sql删除数据库中的所有表语句
·SqlServr利用触发器来控制某...
·SQLServer常用日期格式转换方
·用sql实现查询重复记录的代码
·SQLServer2005分解导入xml文...
热 门 文 章
·图解SQL2008安装向导的使用
·实现Excel数据导入到SQL2005...
·SQLServer常用日期格式转换方...
·MS sql安装时“无法在COM+目...
·sql语句联合删除多个表技巧
·sql删除数据库中的所有表语句
·Sql Server端口连接的问题
·如何在SQL Server中得到自动...
·sql排序规则的简介及其修改操...
·SQL Server2005中删除表中重...
·分析SQL2005最新高效分页方法
·小解sqlServer中临时表及表变...
·用sql实现查询重复记录的代码
·MS SQL2005无法远程连接问题...
·如何修改sqlserver数据库名称
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .