?
Solved

Help with SQL query

Posted on 2013-06-10
7
Medium Priority
?
320 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 Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

777 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