Solved

Help with SQL query

Posted on 2013-06-10
7
318 Views
Last Modified: 2013-06-10
Hello EE,

I have a table wich saves the time and an action.

something like :

SIGN IN      06/06/2013 12:42:01.830
PRESSED     06/06/2013 12:45:11.293
PRESSED     06/06/2013 12:45:41.342
PRESSED     06/06/2013 12:45:11.293
PRESSED     06/06/2013 12:45:41.342
PAUSED      06/06/2013 12:46:21.552
RESUMED   06/06/2013 12:55:01.193
UNPRESSED ....
etc

only possible action are:  sign in, sign out, pressed, unpressed, pause, resume

I want to know how many time in minutes the guy took pause..
here it would be : 12:55:01.193  minus 12:46:21.552

the guy could take multiple pauses.. i need a query that it tells me the total ...

can you help ? thanks...
0
Comment
Question by:PhilippeRenaud
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 22

Expert Comment

by:Om Prakash
ID: 39234397
Try something like:

Select 
FLOOR(Cast(Dummy as Float)) as Days
,DATEPART(Hour,dummy) as Hours
,DATEPART(MINUTE,dummy) as Minutes
,DATEPART(Second,dummy) as Seconds
from
(
Select 
DateAdd(second, SUM(DATEDIFF(SECOND,start,ende)),Cast(0 as Datetime)) as Dummy 
from table_name
) a

Open in new window

http://stackoverflow.com/questions/13697181/sql-query-for-finding-difference-of-two-time-and-sum
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 39234399
Order the dattime field.. Have an Id value as first column..

Whenever paused is found take the id, take the (id + 1)  that is resumed state and take the datetime difference. Below link could help you.

http://stackoverflow.com/questions/2116540/difference-of-two-date-time-in-sql-server
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 39234413
start, ended ?  you writing if I had 2 columns.. but I only have my column called: Action_Date
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 22

Expert Comment

by:Thomasian
ID: 39234417
DECLARE @t TABLE (Act varchar(10), DT datetime)

INSERT @t
SELECT 'SIGN IN', '06/06/2013 12:42:01.830'
UNION ALL SELECT 'PAUSED', '06/06/2013 12:42:11.293'
UNION ALL SELECT 'PRESSED', '06/06/2013 12:43:31.142'
UNION ALL SELECT 'RESUMED', '06/06/2013 12:45:11.295'
UNION ALL SELECT 'PRESSED', '06/06/2013 12:45:41.342'
UNION ALL SELECT 'PAUSED', '06/06/2013 12:46:21.552'
UNION ALL SELECT 'RESUMED', '06/06/2013 12:55:01.193'

;WITH CTE AS (
	SELECT *, rn=ROW_NUMBER() OVER (ORDER BY DT)
	FROM @t
	WHERE Act IN ('PAUSED','RESUMED')
)
SELECT SUM(DATEDIFF(MILLISECOND,T2.DT, T1.DT))/60000.0
FROM CTE T1 INNER JOIN CTE T2 ON T1.rn = T2.rn+1
WHERE T2.Act = 'PAUSED' AND T1.Act = 'RESUMED'

Open in new window

0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 39234433
Thomasian, thanks, is there a way to put the select result into a variable? im trying :  set @total = ( your final select here )  not working..
0
 
LVL 22

Accepted Solution

by:
Thomasian earned 500 total points
ID: 39234440
Yes, just replace the SELECT statement with:
SELECT @total=SUM(DATEDIFF(MILLISECOND,T2.DT, T1.DT))/60000.0

Open in new window

0
 
LVL 1

Author Closing Comment

by:PhilippeRenaud
ID: 39234456
thanks
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question