Solved

SQL Script for time in time format

Posted on 2014-12-17
11
91 Views
Last Modified: 2014-12-22
Hello Experts Exchange
I have a table that has two columns [Tool Code] and [Total Down Time], I want to sum the time in the [Total Down Time] field grouped by the [Tool Code].  The [Total Down Time] column is in the format of 00:00:00.

When I run the following script the [Total Down Time] comes back as a decimal number but I want it to come back as a hour:minute:second format.

Select [Tool Code], Sum(DateDiff(Minute, '0:00:00',[Total Down Time])) as [Total Down Time]
From [dbo].[Acc_Data]
Where [Bay N°] = '3'
and [Reported Date] between '09/12/2014' and '09/12/2014'
Group by [Tool Code]
Order by [Tool Code]

Open in new window


Here is want is returned;
Tool Code      Total Down Time
D250                            78
DM25                            1216
M116                            413
M133                            106
Not Set                             82

I want to see something like this;
Tool Code      Total Down Time
D250                           01:18:00

Regards

SQLSearcher
0
Comment
Question by:SQLSearcher
11 Comments
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
declare @TimeinSecond int=1234;
SELECT RIGHT('0' + CAST(@TimeinSecond / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST((@TimeinSecond / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST(@TimeinSecond % 60 AS VARCHAR),2)

Open in new window


>>> 00:20:34
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
oops, yours in in minutes :) just multiply it by 60 to find second...

or find the difference in second before using the code above... all 59 seconds can make huge difference when summed up :) so be precise, get total in sec first then convert it to hh:mm:ss

if you wish you can use 00 as last part in the code above...

Sum(DateDiff(Minute, '0:00:00',[Total Down Time])) as [Total Down Time]
>>>
Sum(DateDiff(second, '0:00:00',[Total Down Time])) as [Total Down Time]
0
 

Author Comment

by:SQLSearcher
Comment Utility
Hello HainKurt
How do I integrate your code into my query?

Regards

SQLSearcher
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
>The [Total Down Time] column is in the format of 00:00:00.
Q1:  What is the data type of this column?  Date, datetime, varchar...
Q2:  Give us a sample data set to work with.

Offhand, you should be able to sum a time column using SUM(x), or even sum a date column as SUM(CAST(x as time))...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
I think this solution can work for you:
Select [Tool Code], Convert(time,dateadd(minute,Sum(DateDiff(Minute, '0:00:00',[Total Down Time])),0),108) as [Total Down Time]
From [dbo].[Acc_Data]
Where [Bay N°] = '3'and [Reported Date] between '09/12/2014' and '09/12/2014'
Group by [Tool Code]
Order by [Tool Code]

Open in new window

0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
Comment Utility
here:

with x as (
Select [Tool Code], Sum(DateDiff(second, '0:00:00',[Total Down Time])) as [Total Down Time]
From [dbo].[Acc_Data]
Where [Bay N°] = '3'
and [Reported Date] between '09/12/2014' and '09/12/2014'
Group by [Tool Code]
) select [Tool Code], RIGHT('0' + CAST([Total Down Time] / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(([Total Down Time] / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST([Total Down Time] % 60 AS VARCHAR),2) as [Total Down Time (sec)]
from x
Order by [Tool Code]

Open in new window

0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
what happened to the my solutions? no comment? did not work?
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
the one i posted is perfectly fine:

with x as (
Select 'D250' [Tool Code], 78*60 as [Total Down Time]
union select'DM25', 1216*60 
union select 'M116', 413*60
union select 'M133', 106*60
) select [Tool Code], RIGHT('0' + CAST([Total Down Time] / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(([Total Down Time] / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST([Total Down Time] % 60 AS VARCHAR),2) as [Total Down Time (sec)]
from x
Order by [Tool Code]

Tool Code	Total Down Time (sec)
D250	01:18:00
DM25	20:16:00
M116	06:53:00
M133	01:46:00

Open in new window

0
 

Author Comment

by:SQLSearcher
Comment Utility
Hello Experts Exchange
After more work I have found that HainKurt solution is a better query and gives me a better amount of time calculation.


with x as (
Select [Tool Code], Sum(DateDiff(second, '0:00:00',[Total Down Time])) as [Total Down Time]
From [dbo].[Acc_Data]
Where [Bay N°] = '3'
and [Reported Date] between '09/12/2014' and '09/12/2014'
Group by [Tool Code]
) select [Tool Code], RIGHT('0' + CAST([Total Down Time] / 3600 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(([Total Down Time] / 60) % 60 AS VARCHAR),2)  + ':' +
RIGHT('0' + CAST([Total Down Time] % 60 AS VARCHAR),2) as [Total Down Time (sec)]
from x
Order by [Tool Code]

Open in new window


Sorry for the mistake.

Regards

SQLSearcher
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

763 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

9 Experts available now in Live!

Get 1:1 Help Now