sql 查询每月的销售金额


sql 数据分月统计,表中只有每天的数据,现在要求求一年中每个月的统计数据(一条sql

SELECT
  MONTH (  那个日期的字段  ),
  SUM(  需要统计的字段, 比如销售额什么的 )
FROM
  
WHERE
  YEAR (  那个日期的字段  ) = 2010   -- 这里假设你要查 2010年的每月的统计。
GROUP BY
 MONTH (  那个日期的字段  )

2 .求每个月的记录
例:

Create table Counter(
CounterID int identity(1,1)not null,
IP varchar(20),
AccessDateTime datetime
,AccessCount int)

select * from Counter

insert into Counter
select '127.0.0.1',GETDATE(),1 union all
select '127.0.0.2',GETDATE(),1 union all
select '127.0.0.3',GETDATE(),1

 

declare @Year int
set @Year=2015
select m as[Date],
SUM(
case when DATEPART(month,AccessDateTime)=m
then AccessCount else 0 end
) as [Count]
from
Counter c,
(
select 1 m
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 10
union all select 11
union all select 12
)aa
where @Year=YEAR(AccessDateTime)
group by m


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

 

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)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM