任何帮助都会很棒,因为我不熟悉SQL查询:)
谢谢
我有一个名为Registration
的表我想获得每个月注册总数的所有记录。
例如本月是Jun
所以数据会带回来
January 500
February 200
March 600
April 100
May 800
Jun 400
我现在拥有什么
SELECT count(r.regID) AS totalCount
FROM Registration r with(nolock)
WHERE DATEPART(MONTH, createStamp) = DATEPART(MONTH, DATEADD(MONTH, -1, getdate()))
AND DATEPART(YEAR, createStamp) = DATEPART(YEAR, DATEADD(MONTH, -1, getdate()))
现在因为我没有Jun的任何数据,它上个月拉动了
CreatStamp是smalldatetime
答案 0 :(得分:2)
似乎所有你需要的是:
SELECT YEAR(CREATESTAMP), MONTH(CREATESTAMP) COUNT(R.REGID) AS TOTALCOUNT
FROM REGISTRATION R
GROUP BY YEAR(CREATESTAMP), MONTH(CREATESTAMP)
ORDER BY YEAR(CREATESTAMP), MONTH(CREATESTAMP)
你想用DATEPART ... GETDATE()开展什么业务?