Home [SQL] 日期的加工 Datepart()
Post
Cancel

[SQL] 日期的加工 Datepart()

  • 使用Datepart()來取出「年」 、「月」的資料,並進行群組化 group by
    • 取出「年」:DATEPART(YYYY,[處理日]) 年份
    • 取出「月」:DATEPART(MM,[處理日]) 月份
  • 每張傳票都有明細資料,因為要計算傳票數時,必須要在傳票編號上加上distinct (distinct [傳票編號])
    • 計算傳票數:COUNT(distinct [傳票編號])
1
2
3
4
--輸出月份別的傳票數
select DATEPART(YYYY,[處理日]) 年份, DATEPART(MM,[處理日]) 月份, COUNT(distinct [傳票編號]) 傳票數
from [dbo].[販賣資料]
group by DATEPART(YYYY, [處理日]), DATEPART(MM,[處理日])
This post is licensed under CC BY 4.0 by the author.