Demo
每年
select year(ordertime) 年,
sum(Total) 销售合计
from 订单表
group by year(ordertime)
每月
select year(ordertime) 年,
month(ordertime) 月,
sum(Total) 销售合计
from 订单表
group by year(ordertime),
month(ordertime)
每日
select year(ordertime) 年,
month(ordertime) 月,
day(ordertime) 日,
sum(Total) 销售合计
from 订单表
group by year(ordertime),
month(ordertime),
day(ordertime)
Example
sql题 如何统计查询一个月中每天的记录
比如说要查2010年3月份每天的记录
答案
select count(*),substr(t.date,1,10) from table t where t.date like '2010-03%' group by substr(t.date,1,10)
这里date的格式是YYYY-mm-dd hh:mm:ss
sql 数据分月统计,表中只有每天的数据,现在要求求一年中每个月的统计数据(一条sql)
SELECT
MONTH ( 那个日期的字段 ),
SUM( 需要统计的字