Solved

Help with SQL query

Posted on 2013-06-10
7
314 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

25 Experts available now in Live!

Get 1:1 Help Now