I'm writing a database view to sum up a bunch of records where the value in a date column is within the last 7 days. It looks something like this:
CREATE VIEW RecentRecordSum AS
SELECT t.ID,
SUM(t.SomeValue) AS ValueSum
FROM SomeTable t
WHERE t.RecordDate >= DATEADD(d,-7,GETDATE())
GROUP BY t.ID
Is there a way of doing this without having the GETDATE() directly in the where clause?
I'm using SQL Server 2000 and 2005.
Looking at the query plan shows that the cost of the getdate() call is only 0.03% of the entire query (which is considerably more complex than the one above), so performance is not an issue, however I like my queries to be deterministic.
Ideally I'd also like to expose the -7 parameter as a column so that it could be used in the where clause of something querying the view. Currently I'm contemplating a small number of views for 7, 14, 28 day windows.