Solved

Help with SQL query

Posted on 2013-06-10
7
313 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
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this article I will describe the Copy Database Wizard 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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now