从本月,下个月和上个月的数据库中获取结果

时间:2015-12-03 13:46:54

标签: sql date

我需要查看当前月份,下个月和上个月的查询结果。当前月份的查询是:

YOUCOMBOBOX.IsEnabled=false;

我在下个月尝试了SELECT Types.Type, (SELECT COUNT(*) FROM Pitch LEFT JOIN booking ON Pitch.Location_Id = booking.Location_Id WHERE Pitch.Type_ID = Types.Type_ID AND (MONTH(Booking.EndDate) = month(GETDATE()) OR MONTH(Booking.StartDate) = month(GETDATE())) ) AS total FROM Types 月,在上个月尝试了GETDATE()+1,这也有效,但是只有在明年没有进入明年(如果我在2015年12月运行查询,它找不到2016年1月的记录。)

1 个答案:

答案 0 :(得分:0)

管理解决问题

SELECT 
    Types.Type,
    (SELECT 
         COUNT(*) 
     FROM 
         Pitch LEFT JOIN booking ON Pitch.Location_Id = booking.Location_Id
     WHERE 
         Pitch.Type_ID = Types.Type_ID AND 
         (MONTH(Booking.EndDate) = month(DATEADD(MM,1,getdate())) 
          OR MONTH(Booking.StartDate) = month(DATEADD(MM,1,getdate())))
    ) AS total
FROM
    Types