I have an SQL query that selects all records within a 24 hour period, however, I need to update it to select all the records within a month (relative to a records timestamp). So for example if I selected any record within the month of Feb, it should return all records from the beginning to the end of the month.
The code I have to the daily query is:
SELECT Snapshot.xId, MAX(Snapshot.TimeStamp) AS Timestamp_Actual,
dateadd(dd, datediff(dd,0, MAX(Snapshot.TimeStamp)), 0) AS TimestampRange_Start,
dateadd(MI, 1439, dateadd(dd, datediff(dd,0, MAX(Snapshot.TimeStamp)), 0)) AS TimestampRange_End
FROM Snapshot
GROUP BY Snapshot.xId
What I have so far is:
SELECT Snapshot.xId, MAX(Snapshot.TimeStamp) AS Timestamp_Actual,
dateadd(mm, datediff(mm,0, MAX(Snapshot.TimeStamp)), 0) AS TimestampRange_Start,
dateadd(dd, 0, dateadd(mm, datediff(mm,0, MAX(Snapshot.TimeStamp)), 0)) AS TimestampRange_End
FROM Snapshot
GROUP BY Snapshot.xId
From the above statement, I can then include a standard WHERE clause to check the values of TimestampRange_Start and TimestampRange_End to see if Timestamp_Actual falls between them.
1 Answer