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