If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > calculate difference between rows (was "Sql")

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-03-04, 02:52
wasimf wasimf is offline
Registered User
 
Join Date: Mar 2004
Posts: 128
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...
Reply With Quote
  #2 (permalink)  
Old 12-03-04, 10:15
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
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.

Last edited by urquel; 12-03-04 at 10:26.
Reply With Quote
  #3 (permalink)  
Old 12-03-04, 11:08
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally Posted by urquel
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!
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #4 (permalink)  
Old 12-03-04, 11:50
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Quote:
Originally Posted by urquel
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On