create table ST_PPTN_R_Real
(
TID int primary key identity(1,1),
STCD varchar(100) not null,
TM datetime not null,
DRP decimal(8,2) 
)
go
 
insert into ST_PPTN_R_Real values('龙谷站点','2013-05-01 00:00:00.000','52.3')
insert into ST_PPTN_R_Real values('龙谷站点','2013-05-01 00:35:00.000','77.3')
insert into ST_PPTN_R_Real values('龙谷站点','2013-05-01 00:44:00.000','46.4')
insert into ST_PPTN_R_Real values('龙谷站点','2013-05-02 15:21:00.000','85.5')
insert into ST_PPTN_R_Real values('龙谷站点','2013-05-03 05:16:00.000','42.3')
insert into ST_PPTN_R_Real values('龙谷站点','2013-05-03 22:31:55.000','160.77')
insert into ST_PPTN_R_Real values('奇峰峰巅','2013-05-01 02:41:00.000','23.3')
insert into ST_PPTN_R_Real values('奇峰峰巅','2013-05-03 14:50:00.000','564.3')
insert into ST_PPTN_R_Real values('奇峰峰巅','2013-05-05 22:33:00.000','77.5')
insert into ST_PPTN_R_Real values('枉顾站地','2013-05-03 02:01:00.000','43.3')
insert into ST_PPTN_R_Real values('枉顾站地','2013-05-03 05:21:00.000','67.3')
insert into ST_PPTN_R_Real values('枉顾站地','2013-05-06 14:42:00.000','56.4')
insert into ST_PPTN_R_Real values('枉顾站地','2013-05-06 15:21:00.000','83.5')
insert into ST_PPTN_R_Real values('柳体站点','2013-05-14 07:00:00.000','34.3')
insert into ST_PPTN_R_Real values('柳体站点','2013-05-15 09:35:00.000','23.3')
insert into ST_PPTN_R_Real values('柳体站点','2013-05-16 11:44:00.000','62.4')
insert into ST_PPTN_R_Real values('柳体站点','2013-05-16 18:44:00.000','73.4')
insert into ST_PPTN_R_Real values('柳体站点','2013-05-17 09:44:00.000','66.4')
go
 
select * from ST_PPTN_R_Real
go

(一)首先,把条件表准备好:
1.按照每时统计(其中master..spt_values等于master.dbo.spt_values为系统表,number为其中的子段,其实还有更方便的小时统计分组,但这里都套用了一个思路,就直接写下来了)
 
select CONVERT(varchar(20),dateadd(hour,number,cast('2013-04-01 00:00:00' as datetime)),120) rq,
cast(convert(varchar,dateadd(hour,number,cast('2013-04-01 00:00:00' as datetime)),120) as datetime) begin_time,
cast(convert(varchar,dateadd(hour,number+1,cast('2013-04-01 00:00:00' as datetime)),120) as datetime) end_time
from master..spt_values where type='P'

------这里才是真正的重点:
2.按照每天统计(这里的天与所理解的天不太一样,是根据时段设定的天,例如这里从本日的8点开始到次日的8点结束为一天)
 
select convert(varchar(10),cast('2013-04-01 00:00:00' as datetime)+number,120) rq,
cast(replace(convert(varchar,dateadd(d,0,cast('2013-04-01 00:00:00' as datetime)+number),120),'00:00:00','08:00:00') as datetime) begin_time,
cast(replace(convert(varchar,dateadd(d,1,cast('2013-04-01 00:00:00' as datetime)+number),120),'00:00:00','08:00:00') as datetime) end_time
from master..spt_values where type='P'

3.按照每月统计(其实还有更方便的月统计分组,但这里都套用了一个思路,就直接写下来了)
select CONVERT(varchar(7),dateadd(MONTH,number,cast('2013-04-01 00:00:00' as datetime)),120) rq,
cast(convert(varchar,dateadd(MONTH,number,cast('2013-04-01 00:00:00' as datetime)),120) as datetime) begin_time,
cast(convert(varchar,dateadd(MONTH,number+1,cast('2013-04-01 00:00:00' as datetime)),120) as datetime) end_time
from master..spt_values where type='P'

(二)开始分组统计
 
1.按照每时统计

select b.STCD,a.rq,sum(b.DRP) hj from
(
select CONVERT(varchar(20),dateadd(hour,number,cast('2013-04-01 00:00:00' as datetime)),120) rq,
cast(convert(varchar,dateadd(hour,number,cast('2013-04-01 00:00:00' as datetime)),120) as datetime) begin_time,
cast(convert(varchar,dateadd(hour,number+1,cast('2013-04-01 00:00:00' as datetime)),120) as datetime) end_time
from master..spt_values where type='P'
) a
inner join
ST_PPTN_R_Real b
on
b.TM>=a.begin_time and b.TMgroup by STCD,rq order by STCD,rq desc

2.按照每天统计

select STCD,a.rq,sum(b.DRP) hj from
(
select convert(varchar(10),cast('2013-04-01 00:00:00' as datetime)+number,120) rq,
cast(replace(convert(varchar,dateadd(d,0,cast('2013-04-01 00:00:00' as datetime)+number),120),'00:00:00','08:00:00') as datetime) begin_time,
cast(replace(convert(varchar,dateadd(d,1,cast('2013-04-01 00:00:00' as datetime)+number),120),'00:00:00','08:00:00') as datetime) end_time
from master..spt_values where type='P'
) a
inner join
ST_PPTN_R_Real b
on
b.TM>=a.begin_time and b.TMgroup by STCD,rq order by STCD,rq desc

3.按照每月统计

select b.STCD,a.rq,sum(b.DRP) hj from
(
select CONVERT(varchar(7),dateadd(MONTH,number,cast('2013-04-01 00:00:00' as datetime)),120) rq,
cast(convert(varchar,dateadd(MONTH,number,cast('2013-04-01 00:00:00' as datetime)),120) as datetime) begin_time,
cast(convert(varchar,dateadd(MONTH,number+1,cast('2013-04-01 00:00:00' as datetime)),120) as datetime) end_time
from master..spt_values where type='P'
) a
inner join
ST_PPTN_R_Real b
on
b.TM>=a.begin_time and b.TMgroup by STCD,rq order by STCD,rq desc

SqlServer根据时段统计数据的更多相关文章

  1. Sqlserver 按照时间段统计数据

    WITH t1 ( [hour], title ) , ' 0:00:00--- 1:00:00' UNION ALL , ' 1:00:00--- 2:00:00' UNION ALL , ' 2: ...

  2. SQLServer恢复表级数据

    最近几天,公司的技术维护人员频繁让我恢复数据库,因为他们总是少了where条件,导致update.delete出现了无法恢复的后果,加上那些库都是几十G.恢复起来少说也要十几分钟.为此,找了一些资料和 ...

  3. 转载:SQL按照日、周、月、年统计数据的方法

    转载源:http://www.jb51.net/article/42613.htm SQL按照日.周.月.季度.年统计数据的方法 方式一: --按日 select sum(consume),day([ ...

  4. SQLServer比较两条数据是否相同

    SQLServer比较两条数据是否相同 直接比较可能会一个一个字段的比较,也可以将多个字段拼成一个串然后比较,这里有个hash值比较的方法,很好用 用法 将需要的比较的column放进去,逗号隔开,只 ...

  5. 解剖SQLSERVER 第二篇 对数据页面头进行逆向(译)

    解剖SQLSERVER 第二篇  对数据页面头进行逆向(译) http://improve.dk/reverse-engineering-sql-server-page-headers/ 在开发Orc ...

  6. 恢复SQLSERVER被误删除的数据(转——收藏)

    恢复SQLSERVER被误删除的数据 摘自:http://www.cnblogs.com/lyhabc/p/3683147.html 曾经想实现Log Explorer for SQL Server的 ...

  7. sql不重复的查找统计数据(经典)

    例表如下: 表名:MYTEST TID    COL1    COL2     COL3 1           1           A            A2           1     ...

  8. (转载)MySQL 统计数据行数 Select Count

    (转载)http://www.5idev.com/p-php_mysql_select_count.shtml 统计数据行数 SELECT COUNT() FROM 语法用于从数据表中统计数据行数. ...

  9. mysql按月,按日分组统计数据

    group by DATE_FORMAT(createtime,'%Y-%m');//按月统计数据 group by DATE_FORMAT(createtime,'%Y-%m-%d');//按天统计 ...

随机推荐

  1. 【C#】剪切出图片的一部分

    , , , );     Bitmap bmpNew = bmpBase.Clone(rect, bmpBase.PixelFormat);     // 画像をGIF形式で保存     string ...

  2. 云计算的三层SPI模型

    (转自:http://hi.baidu.com/fengjun8216/item/b15bbef4dcf74049922af27b) 一般而言,云计算架构可以用三层SPI模型来表述. 一.基础设施即服 ...

  3. web app变革之rem(手机屏幕实现全适配)

    以往web移动适配,常规写法是:media only screen @media only screen and (min-device-width: 320px){ //针对iPhone 3 } @ ...

  4. Android Studio通过JNI调用NDK程序

    NDK开发,其实是为了项目需要调用底层的一些C/C++的一些东西:另外就是为了效率更加高些,安全性更高. 如果你在Eclipse+ADT下开发过NDK就能体会到要么是配置NDK还要下载Cygwin,配 ...

  5. Java IO 之 InputStream源码

    Writer      :BYSocket(泥沙砖瓦浆木匠) 微         博:BYSocket 豆         瓣:BYSocket FaceBook:BYSocket Twitter   ...

  6. Xenia and Weights(深度优先搜索)

    Xenia and Weights time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. base.js

    function $_id(id){return document.getElementById(id)};//$只定义为通过ID返回元素的功能 //-----------------------do ...

  8. Web Component--01. 简介

    Web Components 是什么? Web Components是W3C定义的新标准,它给了前端开发者扩展浏览器标签的能力,可以自由的定制组件,更好的进行模块化开发,彻底解放了前端开发者的生产力. ...

  9. 转:Android开发环境搭建

    一.软件.开发包清单 Android开发首先需要搭建完整的开发环境,由于软件均是基于Dalvik Java Virtual Machine运行的所以Google官方推荐使用主流的Java开发IDE E ...

  10. 在C函数中保存状态:registry、reference和upvalues

    C函数可以通过堆栈来和Lua交换数据,但有时候C函数需要在函数体的作用域之外保存某些Lua数据,那么我们想到全局变量或static变量,这样做的缺点是:(1)为Lua设计C函数库时,导致不可重入:(2 ...