tags:

views:

21

answers:

1

I want to get the difference of two values within a time frame.

so I have a table like this

Data Table

TimeStamp        DataValue
2010-06-01        21
2010-06-03        33
2010-06-05        44

So I want to first get all data over the last month which I can do with something like.

([TimeStamp] < GETDATE()-0 and ([TimeStamp] > GETDATE()-31)

But I want to see how much value added on over the course of the month. So it started at 21 and went to 44. So I would expect this example to to return 23 (as in 44-21).

How would I build a query like this?

+1  A: 

I know this works in MySQL. I can't promise for other databases.

SELECT MAX(DataValue) - MIN(DataValue) FROM TABLE
WHERE ([TimeStamp] < GETDATE()-0
AND ([TimeStamp] > GETDATE()-31)
CoreyD
This should work, but only if you have the lowest value when the month starts, and the highest value at the end of the month.
Peter Lang
I was trying to make this too complicated. Thinking I would somehow have to get the first value alias it and all.
Maestro1024
That would be another way of doing it. And that way might be necessary if the values could go either up or down. I assumed from your language in the question that they could only increase over time.
CoreyD