Tuesday, March 20, 2012

calculate difference between rows (was "Sql")

Hi all,
If I have the table:

Date/Time DeviceNumber X
2/12/04 07:15:15 40 20
2/12/04 12:10:02 40 60
2/12/04 17:56:03 40 100
3/12/04 06:12:24 40 500
3/12/04 08:19:15 40 700

How I can calculate the difference of X where the first X was at the minimum time in 3/12/04 and the second X was at the minimum time in 2/12/04.

I mean the two rows:
3/12/04 06:12:24 40 500
2/12/04 07:15:15 40 20

The X difference is: 480

Please help
Thanks...Is it me, or does this seem like deja vu.

Try:

Select sum(xx)
from(
select x as xx
from table
where Date/Time = (select max(Date/Time) from table)
union
select multiply(x,-1) as xx
from table
where Date/Time = (select min(Date/Time) from table)
)

The syntax will have to be modified to fit your DBMS and correct any errors i may have included.|||Is it me, or does this seem like deja vu.
Yes, this question does seem to be posted on a daily basis at the moment!|||Is it me, or does this seem like deja vu.As George Carlin put it, "It's not just Deja Vu, it is Deja Vu all over again!"

-PatP

No comments:

Post a Comment