SQL语句统计每天、每月、每年的销售总额

来源:百度知道 编辑:UC知道 时间:2024/05/26 09:49:38
SQL语句统计每天、每月、每年的销售总额

表 订单 里有字段 Ordertime datetime(订单时间),Total Decimal(10,2)(订单总额)
现在要根据订单里的时间来统计每天、每月、每年的销售总额

要求:一般人能看懂的sql语句,别要有存储过程啊,游标之类的。。

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 sum(case when DatePart(m,订单时间)=1 then 订单总额 else 0 end),sum(case when DatePart(m,订单时间)=2 then 订单总额 else 0 end),...sum(case when DatePart(m,订单时间)=12 then 订单总额 else 0 end) From 表 Where ...

其他的类似

也可以单独求
Select sum