Maestro1024
Maestro1024

Reputation: 3293

getting values by time difference in SQL

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?

Upvotes: 1

Views: 86

Answers (1)

CoreyD
CoreyD

Reputation: 220

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)

Upvotes: 1

Related Questions