SQL按照日、周、月、年、时间段统计数据

--按日

select sum(consume),day([date]) from consume_record where year([date]) = ‘2006‘ group by day([date])

--按周quarter

select sum(consume),datename(week,[date]) from consume_record where year([date]) = ‘2006‘ group by datename(week,[date])

--按月

select sum(consume),month([date]) from consume_record where year([date]) = ‘2006‘ group by month([date])

--按季

select sum(consume),datename(quarter,[date]) from consume_record where year([date]) = ‘2006‘ group by datename(quarter,[date])

--按年

select sum(consume),year([date]) from consume_record where  group by year([date])

--这个按每五分钟的

[code=SQL]create table tb(时间 datetime , 金额 int)

insert into tb values(‘2007-1-1 10:00:23‘ ,          8 )

insert into tb values(‘2007-1-1 10:01:24‘ ,          4 )

insert into tb values(‘2007-1-1 10:05:00‘ ,          2 )  

insert into tb values(‘2007-1-1 10:06:12‘ ,          3 )

insert into tb values(‘2007-1-1 10:08:00‘ ,          1 )

insert into tb values(‘2007-1-1 10:12:11‘ ,          5 )

go

--时间段>=10:00:00 and 时间段<10:05:00

select dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,-1,时间),120),dateadd(ss,-1,时间))/5)*5,convert(varchar(10),时间,120)) as 时间段,

       count(*) as 行数,

       sum(金额) as 总金额

from tb

group by dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,-1,时间),120),dateadd(ss,-1,时间))/5)*5,convert(varchar(10),时间,120))

/*

时间段                                                    行数          总金额         

------------------------------------------------------ ----------- ----------- 

2007-01-01 10:00:00.000                                3           14

2007-01-01 10:05:00.000                                2           4

2007-01-01 10:10:00.000                                1           5

(所影响的行数为 3 行)

*/

--时间段>10:00:00 and 时间段<=10:05:00

select dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,1,时间),120),dateadd(ss,1,时间))/5)*5,convert(varchar(10),时间,120)) as 时间段,

       count(*) as 行数,

       sum(金额) as 总金额

from tb

group by dateadd(mi,(datediff(mi,convert(varchar(10),dateadd(ss,1,时间),120),dateadd(ss,1,时间))/5)*5,convert(varchar(10),时间,120))

/*

时间段                                                    行数          总金额         

------------------------------------------------------ ----------- ----------- 

2007-01-01 10:00:00.000                                2           12

2007-01-01 10:05:00.000                                3           6

2007-01-01 10:10:00.000                                1           5

(所影响的行数为 3 行)

*/

drop table tb

时间: 2024-10-13 23:16:56

SQL按照日、周、月、年、时间段统计数据的相关文章

Flex自定义组件开发之日周月日期选择日历控件

原文:Flex自定义组件开发之日周月日期选择日历控件 使用过DateField的我们都知道,DateField 控件是用于显示日期的文本字段,字段右侧带有日历图标.当用户在控件边框内的任一位置单击时,将弹出一个 DateChooser 控件,显示当月的所有日期.如果未选择日期,则该文本字段为空白,并且 DateChooser 控件中将显示当前日期的月份.当 DateChooser 控件处于打开状态时,用户可以在各个月份和年份之间滚动,并选择某个日期.选择日期后,DateChooser 控件关闭,

SQL周、日、月、年数据统计

本文只是记录在项目中用到的统计的SQL语句,记一笔以防忘了 /// <summary> /// 获取统计数据 /// </summary> /// <param name="CKEY">店面ckey</param> /// <param name="type">统计类型(日.周.月.年)</param> /// <returns></returns> [WebMethod

获得自然日周月时间

1.以下函数都可以直接使用. function getReportDates($periods){ $dates = array(); //当前时间周期的起止时间 $end = getWeekendByDate($periods); $start = date("Y-m-d",strtotime("-".($periods-1). "day",strtotime($end))); //获取日期($times个周期) $dates['start']

sql语句分别按日,按周,按月,按季统计金额

如: 表:consume_record 字段:consume (money类型) date (datetime类型) 请问怎么写四条sql语句分别按日,按周,按月,按季统计消费总量. 如:1月 1200元 2月 3400元 3月 2800元 --按日 select sum(consume),day([date]) from consume_record where year([date]) = '2006' group by day([date]) --按周quarter select sum(

MySql按周/月/日分组统计数据的方法

知识关键词:DATE_FORMAT select DATE_FORMAT(create_time,'%Y%u') weeks,count(caseid) count from tc_case group by weeks; select DATE_FORMAT(create_time,'%Y%m%d') days,count(caseid) count from tc_case group by days; select DATE_FORMAT(create_time,'%Y%m') month

MySql 按周/月/日统计数据的方法

知识关键词:DATE_FORMAT select DATE_FORMAT(create_time,'%Y%u') weeks,count(caseid) count from tc_case group by weeks; select DATE_FORMAT(create_time,'%Y%m%d') days,count(caseid) count from tc_case group by days; select DATE_FORMAT(create_time,'%Y%m') month

用sql语句按周、按月、按季、按年统计

--按mySql语法统计按周,月,季,年.income为合计的价格字段,createDate为交易时间. select sum(income)as revenue,week(createDate) as week,month(createDate) as month, quarter(createDate)as quarter,year(createDate)as year from employee where year(createDate) >= '2006' group by week(

MySQL统计函数记录——按月、按季度、按日、时间段统计

按年汇总,统计:select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%Y');按月汇总,统计: select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%Y-%m');按季度汇总,统计: select sum(mymoney) as

用sql语句按周、按月、按季、按年统

原文地址:http://hi.baidu.com/%BD%F0%D3%F1kl_y/blog/item/1c368ffba9388476024f5645.html --按mySql语法统计按周,月,季,年.income为合计的价格字段,createDate为交易时间. select sum(income)as revenue,week(createDate) as week,month(createDate) as month, quarter(createDate)as quarter,yea