• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 735
  • Last Modified:

SQL QUERY subtraction query

I need to preform subtraction on a table and display the difference.
I need to subtract 2 times

id        Date
1         2012-04-20 10:48:15.363
2         2012-04-20 10:48:15.383
3         2012-04-20 10:48:37.643
4         2012-04-20 10:48:37.793


I need to subtract id 2 Date - id 1 Date
which is .20
0
Leo Torres
Asked:
Leo Torres
1 Solution
 
tim_csCommented:
If you are just doing two specific IDs you could do something like this.


SELECT
   CAST((SELECT date FROM YourTable WHERE ID = 1) - (SELECT Date FROM YourTable WHERE ID = 2) AS Time)
0
 
lwadwellCommented:
You could also do
SELECT a.id, DATEDIFF(ms, b.Date, a.Date)
FROM yourtable a
INNER JOIN yourtable b ON a.id + 1 = b.id

if only two rows

SELECT a.id, DATEDIFF(ms, b.Date, a.Date)
FROM yourtable a
INNER JOIN yourtable b ON a.id + 1 = b.id
WHERE a.id = 1
0
 
Leo TorresSQL DeveloperAuthor Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now