Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Time Subtraction

Author  Topic 

syed498
Starting Member

5 Posts

Posted - 2014-06-28 : 01:30:04
Hi Team,

I am new to SQL. I have a small query which if addressed will be a great help. I have Microsoft SQL Server 2012.

I have a table as below:

Column1 Column2 Time Status
1 Name1 2014-05-21 07:34:21.490 Status1
2 Name1 2014-05-21 15:52:19.043 Status2
3 Name2 2014-05-21 15:54:08.457 Status1
4 Name2 2014-05-21 16:02:33.900 Status2
5 Name3 2014-05-21 04:06:29.027 Status1
6 Name3 2014-05-21 04:24:34.850 Status2
7 Name4 2014-05-21 04:34:35.050 Status1
8 Name4 2014-05-21 04:34:37.045 Status2

Now I want a result which subtracts Time of Status2 from Time of Status1 and gives me a result as below:

Column1 Difference
Name1 Difference of Status2 minus Status1 time in Minutes
Name2 Difference of Status2 minus Status1 time in Minutes
Name3 Difference of Status2 minus Status1 time in Minutes
Name4 Difference of Status2 minus Status1 time in Minutes

Let me know if you need any more clarity.

Thanks in Advance,
Rushad

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-06-29 : 23:42:20
assuming that your records are in pair

SELECT Column2, DATEDIFF(MINUTE, MIN(Time), MAX(Time))
FROM yourtable
GROUP BY Column2



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -