存储过程 每15分钟分配一个区间 并相加

本文介绍了一种使用SQL进行批量时间区间统计的方法。通过创建临时表并对数据按小时和15分钟段进行分组,实现了对指定日期下拍卖数量的时间分布统计。此过程涉及多表联接和条件筛选,为数据分析师提供了一种实用的数据处理思路。
  ALTER proc [dbo].[sp_hyfx_SoldOutDistributiones] 
(@thedate bigint)
as
begin
--   create table #houres 
--(houres int )
--declare @i int 
--set @i=1
--while @i<=24
--begin 
--insert #houres values (@i-1)
--set @i =@i+1
--end 


select datepart( hour , OperationDate ) as houres, datepart( minute , OperationDate ) as minutess,AuctionNum,  OperationDate ,thedate into #hhh from   hyfx_SoldOutDistribution  where thedate=@thedate
  
  select '1-15' AS times , houres ,sum(AuctionNum) as AuctionNum into #tb1   from #hhh where thedate=@thedate AND MINUTESS  BETWEEN 1 AND 15 group by houres 
  UNION
  select '16-30' AS times , houres ,sum(AuctionNum) as AuctionNum   from #hhh where thedate=@thedate AND MINUTESS  BETWEEN 16 AND 30 group by houres 
   UNION
   select '30-45' AS times , houres ,sum(AuctionNum) as AuctionNum  from #hhh where thedate=@thedate AND MINUTESS  BETWEEN 30 AND 45 group by houres 
      UNION
   select '45-60' AS times , houres ,sum(AuctionNum) as AuctionNum  from #hhh where thedate=@thedate AND MINUTESS  BETWEEN 45 AND 60 group by houres 
  --   select T.houres ,cast( isnull( a.AuctionNum ,0) as varchar)  +'+' +cast ( isnull(b.AuctionNum ,0) as varchar)+'+' +cast ( isnull(c.AuctionNum ,0) as varchar)+'+' +cast ( isnull(d.AuctionNum ,0) as varchar)  as section
--from #houres t left join  #tb1 a  on  t.houres=a.houres and a.times ='1-15'  left join #tb1 b on t.houres =b.houres and b.times='16-30' left join #tb1 c on  t.houres=c.houres and c.times='30-45' left join #tb1 d on  t.houres=d.houres
--and d.times='45-60'




   select distinct a.houres ,cast( isnull( a.AuctionNum ,0) as varchar)  +'+' +cast ( isnull(b.AuctionNum ,0) as varchar)+'+' +cast ( isnull(c.AuctionNum ,0) as varchar)+'+' +cast ( isnull(d.AuctionNum ,0) as varchar)  as section
from   #tb1 a   left join #tb1 b on a.houres =b.houres and b.times='16-30' left join #tb1 c on  a.houres=c.houres and c.times='30-45' left join #tb1 d on  a.houres=d.houres 
and d.times='45-60'  and a.times ='1-15'  
--select * from  #hhh
  --select distinct a.houres ,cast( a.AuctionNum as varchar)  +'+' +cast (b.AuctionNum as varchar)+'+' +cast (c.AuctionNum as varchar) +'+' +cast (d.AuctionNum as varchar)   from #tb1 a  ,#tb1 b ,#tb1 c,#tb1 d where a.houres =b.houres and a.houres=c.houres and a.houres=d.houres and a.times ='1-15' and b.times ='16-30' and c.times ='30-45' and d.times ='45-60'
 
  end;


     exec sp_hyfx_SoldOutDistributiones 20171018
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值