如何从数据库中获取上个月的数据

时间:2012-03-05 19:14:17

标签: sql sql-server

我有一个用于存储用户记录的表

这包括2列applied_date.js_id.

现在我必须计算js_id已经适用于本月的工作数量。

3 个答案:

答案 0 :(得分:2)

类似的东西:

SELECT COUNT(js_id), MONTH(applied_date)
FROM table
GROUP BY MONTH(applied_date)

答案 1 :(得分:2)

对于特定月份(给定日期),

DECLARE @date SMALLDATETIME = '20120105'; -- for January, also could use CURRENT_TIMESTAMP
-- the above could also be a stored procedure parameter

SET @date = DATEADD(MONTH, DATEDIFF(MONTH, 0, @date), 0);

SELECT COUNT(js_id) 
  FROM dbo.[table_name]
  WHERE applied_date >= @date
  AND applied_date < DATEADD(MONTH, 1, @date);

答案 2 :(得分:0)

Select count(js_id) from yourtable   WHERE DATEDIFF( m, applied_date, GETDATE() ) = 0