一、SQL语句统计每年的销售总额
select year(ordertime) 年,
sum(Total) 销售合计
from 订单表
group by year(ordertime)
二、SQL语句统计每月的销售总额
select year(ordertime) 年,
month(ordertime) 月,
sum(Total) 销售合计
from 订单表
group by year(ordertime),
month(ordertime
三、SQL语句统计每日的销售总额
select year(ordertime) 年,
month(ordertime) 月,
day(ordertime) 日,
sum(Total) 销售合计
from 订单表
group by year(ordertime),
month(ordertime),
day(ordertime)
扩展资料
mysql查询每天、每周、每月的数据方法
一、查询每天的数据
SELECT
COUNT(1) AS countNumber,
DATE_FORMAT(createTime,'%Y-%m-%d') AS dateTime
FROM
testTable
GROUP BY DATE_FORMAT(createTime,'%Y-%m-%d')
二、查询每周的数据
SELECT
COUNT(1) AS countNumber,
WEEK(createTime) as dateTime
FROM
testTable
GROUP BY WEEK(createTime)
三、查询每月的数据:
SELECT
COUNT(1) AS countNumber,
MONTH(createTime) as dateTime
FROM
testTable
GROUP BY MONTH(createTime)
参考资料:
百度百科SQL语句大全
SQL语句统计每天、每月、每年的销售总额具体如下:
1、每年
2、每月
3、每日
另外一种计算每天销售额的算法:
总结:
重点注意事项:
1、每年
select year(ordertime) 年,
sum(Total) 销售合计
from
订单表
group by year(ordertime)
2、每月
select year(ordertime) 年,
month(ordertime) 月,
sum(Total) 销售合计
from
订单表
group by year(ordertime),
month(ordertime
3、每日
select year(ordertime) 年,
month(ordertime) 月,
day(ordertime) 日,
sum(Total) 销售合计
from
订单表
group by year(ordertime),
month(ordertime),
day(ordertime)
另外每日也可以这样:
select convert(char(8),ordertime,112) dt,
sum(Total) 销售合计
from
订单表
group by convert(char(8),ordertime,112)
如果需要增加查询条件,在from后加where 即可。
非常简单--每天select convert(varchar(10),Ordertime,120) as '订单日期',sum(订单总额) as '销售总额' from 表 group by convert(varchar(10),Ordertime,120) order by convert(varchar(10),Ordertime,120)--每月select convert(varchar(7),Ordertime,120) as '订单日期',sum(订单总额) as '销售总额' from 表 group by convert(varchar(7),Ordertime,120) order by convert(varchar(7),Ordertime,120)--每年select convert(varchar(4),Ordertime,120) as '订单日期',sum(订单总额) as '销售总额' from 表 group by convert(varchar(4),Ordertime,120) order by convert(varchar(4),Ordertime,120)
你这个需要分三次查询了,如果你的时间存储显示是2017-10-16 11:00:00,MYSQL语法:
-- 每年
select left(Ordertime,4) as day,sum(Total) from table;
-- 每月
select left(Ordertime,7) as day,sum(Total) from table;
-- 每天
select left(Ordertime,10) as day,sum(Total) from table;