1. 程式人生 > >SQL 按月統計(兩種方式)

SQL 按月統計(兩種方式)

(1)Convert 函式

select Convert ( VARCHAR(7),ComeDate,120) as Date ,Count(In_code) as 單數,Sum(SumTrueNum) as 件數 

from T_In_Top 
where  ComeDate between '2013-01-01' and '2014-08-04' and In_top_State='已完成' and Case_ID=14  and Store_ID=41
group by Convert ( VARCHAR(7),  ComeDate,  120)

(2)日期函式 year([date])

select year(ComeDate),month(ComeDate),Count(In_code) as 單數,Sum(SumTrueNum) as 件數
from  T_In_Top
where Case_ID=14 and In_top_State='已完成' and Store_ID=41 and year(ComeDate)>=2013

group by month(ComeDate),year(ComeDate) 

--(cast(year(ComeDate) as varchar(4))) cast 函式 型別轉換