# Summing Time Span (for Total Per Hour)

Sep 12, 2013

field prodDate puts out values as such:

8/12/2013 7:50:15
8/12/2013 7:51:03
8/12/2013 7:53:42
8/12/2013 7:54:12
8/12/2013 7:56:02
...
...
8/12/2013 14:57:57
8/12/2013 14:59:59

I'd like to get a time span sum beginning with the first prodDate entry and ending with the last.

The idea is I'll then use that to divide lbs produced, thus giving me a gauge for lb per hour.

## Calculate Elapsed Time Between Dates And Exclude A Time Span.

Dec 18, 2007

I need a formula to calculate the time (let's say in minutes) between two dates/times.
The problem is that I have to exclude the time between 06 PM and 06 AM and also exclude the time in the weekend (Saturday and Sunday).
I will use this in a couple of reports made in Reporting Services.
If anyone have an algoritm that could be modified for this and is willing to share this I would be very grateful.
Many thanks!
/Per Lissel

## Running Total: Summing On Distinct Amount

May 9, 2007

I am trying to do a sum on a goal amount that is repeated for each record. But what is the the forumla to only sum on the distinct goal amount.

Example:

Month Year Goal Other Value

March 2007 500 5568

March 2007 500 5568

March 2007 500 5569

April 2007 600 5568

April 2007 700 5569

Total (I am receive)

March-April 2007 1600 5568

March-April 2007 1200 5569

Total (I excpect)

March-April 2007 1100 5568

March-April 2007 1200 5569

I haven't found anything online to help.

Thanks,

Sam

## Group By Time Span

Feb 16, 2006

I have a database where user events are recorded quite frequently. I'd like to be able to get a count of the 'good' events that happen in each 5 second period. Unfortunately I don't know how to display and group by a time range.

Here is the query I would like to change:
SELECT count(*), clientTime
FROM dbo.V_COMBINED
WHERE (sessionId = '122b') AND (type = N'sys_goodaction') AND (paraName = 'value')
GROUP BY clientTime

It returns records like:
1 |2006-02-16 23:21:05.250
1 |2006-02-16 23:21:05.267
1 |2006-02-16 23:21:06.470

I'd like it to return records like:
5 |2006-02-16 23:21:06 - 23:21:10
3 |2006-02-16 23:21:11 - 23:21:15
4 |2006-02-16 23:21:16 - 23:21:20

Anyone know how I could do this? Is it even possible?

Thanks

## How To Compare One Time Span To Another One

Nov 18, 2013

I have to compare two time duration for a resource booking system. for example if a resource is booked for 9.00am to 2.00am ,it must not be booked during that duration and other coinciding times e.g 1.00pm to 2.00 pm should not be booked or even 9.00 am to 5.00pm should not be booked.

how can i write a sql query for this.

## How To Retrieve Time Span??

Sep 18, 2007

Hello,

Some advice appreciated on the following, thanks!
I have this query:
select field1, field2, astartdate, anenddate
from atable
where astartdate BETWEEN '9/11/2007 12:00:00 AM' AND '9/18/2007 11:59:59 PM'

I need the results to be like the following:
field1,field2,anenddate-astartdate <<that's minus

Thank you for the help!
--PhB

--PhB

## How To Exact Span Of Time From A Record

Aug 23, 2012

I am not sure how to go about doing this. I have a record that has a start time of 1 am and a stop time of 9pm (same day for simplicity) and I want to know how many hours during a peek time and how many were not.

For example, the application starts and inserts into the data base the start time of 1am and then the user stops the app at 9pm. Lets say the peek hours are 1pm to 7pm. I know i can do a date diff function to get how long the app ran for but how can I get the amount of hours it ran during the peek time? I know there has to be some mathematical solution to this but it is escaping me at the moment. I want to do this over many records so a a cte or pivot table is the end solution for performance.

## Transact SQL :: How To Get Time Span Across Several Related Records

Nov 19, 2015

I need a boost to understand how to get started on solving this SQL Query.

TicketId Seq StatusDateTime

T10001 1 2015-05-04 15:54:00.000
T10001 2 2015-05-04 16:35:00.000
T10001 3 2015-05-04 16:42:00.000
T10001 4 2015-05-05 08:19:00.000
T10002 1 2015-05-04 15:56:00.000
T10002 2 2015-05-04 15:57:00.000

I need to find the total StatusDateTime for each TicketId
I need to find the average StatusDateTime for all TicketIDs
Ex. TicketId, "T10001", has 4 records based on the Seq column.

By using this, I should be able to find the amount of time between the first Seq and the last Seq to get a total time span for Ticket.

Expanding on this, I should be able to add up all of the Ticket's calculated time spans and divide by the number of tickets to get the average time span.

## Output Every Hour Sales Total By Different Shop

Sep 1, 2014

I desire output by each hour of sales by different shop total with sales_datetime format is 20140831 22:30:xxx.

for example now is 23:05 then desire result is 23:00-23:05 of sale.
-----------------------------------------------------------------
select shopid,qty,amount from sales where sales_datetime>='hh:mm' and sales_datetime<='hh:mm' group by shop

## SQL Server 2012 :: Splitting Time Span Into Multiple Entries?

Jul 10, 2014

Looking for returning multiple entries from a time span. I have a date, start-time, end-time and duration. I need the start-times separated in a list. It's fine if temp tables are needed - I have that clearance.

Entry might look like:

(datetime) date: 7/10/2014

(int) start-time 820

(int) end-time 1000

(int) duration 20

The result would needed:

(datetime) 7/10/2014 08:20:00

(datetime) 7/10/2014 08:40:00

(datetime) 7/10/2014 09:00:00

(datetime) 7/10/2014 09:20:00

(datetime) 7/10/2014 09:40:00

## SQL Server 2014 :: Split Time Span Into Multiple Records?

Oct 30, 2015

I have a table that includes the fields below:

ID - INT
Machine - TINYINT
StartTime - DATETIME
EndTime - DATETIME

What I am trying to do is figure out how much time is used for production per day. The problem is, there are production runs that run over midnight and possible multiple days without ending. For example, if I have the following data:

ID - 1
Machine - 2
StartTime - 2015-09-01 22:00:00.000
EndTime - 2015-09-03 22:00:00.000

So what I am looking for is taking the above record and turning it into 3 records like below:

ID Machine StartTime EndTime
1 2 2015-09-01 22:00:00.000 2015-09-01 23:59:59.999
1 2 2015-09-02 00:00:00.000 2015-09-02 23:59:59.999
1 2 2015-09-03 00:00:00.000 2015-09-03 22:00:00.000

## Total Page Writes/total Amount Of Data Change In A Set Period Of Time

Jun 9, 2004

Does anyone know how I can determine the number of page writes that have been performed during a set period of time? I need to figure out the data churn in that time period.

TIA

## Rond Time Down To Hour

Aug 5, 2007

I want to round times down to the previous hour,
For example a call time of 7/31/07 11:51:13.532 would become 7/31/07 11:00:00.000.

## Gurus Help!! How To Sort By Hour And By Day Of Time?

Jan 21, 2005

I need to query data from a table which has order_date which is datetime field. How to pull orders by hour by day per month for the last 4 months. Any idea how to write the query?

Sample data:

order_dt order_no
2004-02-04 18:18:19.800 100
2004-02-04 18:18:22.677 101
2004-02-05 06:30:23.300 102
2004-02-05 06:31:39.533 103
2004-02-05 06:32:45.377 104

## 24 Hour Format And Current Time

Sep 3, 2014

if now the system time is 2014-12-23 23:45:345 . then I want to calculate the sales amount with total and get current date and current hour filter by hh:00-hh:59 , my desire output is like below :

Current time:23:50 run this query
Date Time amount
2014-12-23 23:40 \$10
2014-12-23 23:01 \$5
2014-12-23 23:39 \$5
Total :\$20

## Time Interval For Hour And 30 Minutes

Sep 4, 2007

Time Interval for hour and 30 minutes
--------------------------------------------------------------------------------

Hi Everyone

I Have column in sql server databas as "HHMMSS" and data as and i am doing a substring to get values for hours and minutes. since my calculations based on hour interval and 30 minutes interval

for ex: Now i want to show all the transaction done b/w 6 to 7 am or pn.

and for 30 minutes interval i have get the calculation as transactions done b/w 6:00 to 6:30 and 6:30 to 7:00 either it's am or pm. now how i can write my sql statements that calculates hour and 30 minutes intervals
HHMM
-------------
06:43
09:26
09:26
11:58
12:25
18:17
20:45
00:43
00:53
16:47

Thanks
Phani

## Convert Time To 24 Hour Clock

May 30, 2007

All ,

I need to convert the given time into 24 hour clock .

I have the two tables out of which one contain time in 12 Hr clock and another contain time in 24 Hr clock and i need to make a join on this colum by converting 12 Hr time in to 24 hr clock time.

Regards,

Ashish

## How To Find The Hour Of A Date Time

Oct 22, 2007

i have a query i need to calulate the HH of a datetime field

example

Dateval Need to get

10/01/2007 7:48
10/01/2007 7:00

10/01/2007 7:49
10/02/2007 7:00

10/01/2007 7:50
10/03/2007 7:00

10/01/2007 7:51
10/04/2007 7:00

10/01/2007 7:52
10/05/2007 7:00

10/01/2007 7:53
10/06/2007 7:00

10/01/2007 7:54
10/07/2007 7:00

10/01/2007 7:55
10/08/2007 7:00

## Adding Half Hour Intervals To Time

Oct 6, 2014

I have data that looks like the following.

ID Date Time Length Interval_Num
1 10/11/2014 9:00 420 14

Basically, length represents the # of minutes a person is scheduled for. We have 30 minute intervals. Interval_Num = (Length/30). I need the data to show each interval. For the above example it should only be 14 intervals.For the above example the solution should look like....

ID Date Time Length
1 10/11/2014 9:00 30
1 10/11/2014 9:30 30
1 10/11/2014 10:00 30
1 10/11/2014 10:30 30

for 14 intervals up until 16:00

## Converting Varchar Time HHMM To 24 Hour HH:MM:SS?

Oct 29, 2014

How can I do this? I have a column that is a varchar and times are stored like this:

0600
1240
0145
2335

How can I get those to HH:MM formats? I've tried this but I don't know if it's the best way, plus there are seven 0s after the MM:

convert(time,LEFT(b.status_time,2) +':'+ RIGHT(b.status_time,2))

## How To Convert A Time-stamp To Half-hour?

Nov 16, 2007

I would like to make a function to convert a datetime to half-hour. E.g. If the timestamp is 1:23:05 then converts to 1:30:00, if 1:35:27 then converts to 2:00:00.

Anyone has any idea? Thanks.

## Reporting Services :: Round Time To Quarter Hour

Jun 2, 2011

SSRS 2008 R2. I am writing a report that uses a DB4 database. Because of this many SQL commands that I normally use are not available and I don't know what commands I can use. Because of this I am doing much of my calculations inside of SSRS.

I need to round a time field to the nearest quarter hour inside of SSRS.

For example:
8:12 becomes 8:15
2:30 becomes 2:30
1:57 becomes 2:00
4:07 becomes 4:00

## I Have A Web Site Where I Input Stories And Set The Publish Date To Some Time In Future And Set An Hour From 1-24....

Sep 20, 2007

i need to be able to do an sql call whereby i can check the day and hour for publishing.eg select story where day = ?? AND hour = ?? any one know the best way to do this.
i need to call out date as a day number or someting and the getdate as just the hour??? anyone know how??

## Power Pivot :: Calendar Look Up Table - Custom 24 Hour Start Time

Jul 9, 2015

I know how to do this in Excel, but not sure about Power Pivot. Our fire department works on a 24 hour shift that starts at 0700 each morning. I want to look up what shift is working depending on the date and time of dispatch. In Excel, I would do a look up table, but instead of using false() as  the last parameter, I would choose true().

I am not sure if this would be best created as a calculated column in the table that holds the incident date and time values, or have a separate table and create a relationship to use the shift in the rows/columns of my pivot table.

## Counting Popularity, Hour By Hour

Nov 14, 2001

I have a table that is recording hits to a website. Everytime someone views a page, the datetime of the hit is recorded in a field called hit_date_time. I would like to be able to come up with a query that will show how many hits occured on a given day or given days, broken down by hour.

The resulting table for two days would look something like:
Time Hits
1/1/01 12:00 1
1/1/01 1:00 23
1/1/01 2:00 54
1/2/01 1:00 15
1/2/01 2:00 14

I can't seem to figure out how to write the query so that I can take into consideration the date and hour of the event so that I can count it.

Thanks,

Eron

## Run-Time Running Total *(SOLVED)

May 14, 2008

I am re-writing a old query that update's it's value based on values at run time. Is there anyway to accomplish the following query where column s1.AI referances the values that were updated during run-time.

I want to avoid a Loop (i.e. cursor or a CTE).

Declare @Stage Table(StartDate datetime,BenefitInterestID INT PRIMARY KEY, Amount MONEY, InterestAmount MONEY, Interest DECIMAL(10, 4), ai DECIMAL(10, 4))
Insert Into @Stage
Select
convert(datetime,'2006-12-01 00:00:00.000',101) as StartDate,1 as BenefitInterestID,1701.00 as amount,79.605 as InterestAmount ,0.1000 as Interest,0.0000 as ai
Union all
select '2007-12-01 00:00:00.000',2,172.80,7.92,0.0500,0
Union all
select '2008-12-01 00:00:00.000',4,0.00,0.00,0.0700,0

SETs1.ai = s1.Interest * coalesce((SELECT SUM(coalesce(s2.Amount,0) + coalesce(s2.InterestAmount,0)+coalesce(s2.ai,0)) FROM @Stage AS s2 WHERE s2.StartDate < s1.StartDate ),0)

FROM@Stage AS s1

select * from @Stage

My desired Results are

2006-12-01 00:00:00.00011701.0079.6050.10000.0000
2007-12-01 00:00:00.0002172.807.920.050089.0303
2008-12-01 00:00:00.00040.000.000.0700143.5300

But I am geting

2006-12-01 00:00:00.00011701.0079.6050.10000.0000
2007-12-01 00:00:00.0002172.807.920.050089.0303
2008-12-01 00:00:00.00040.000.000.0700137.2928

Due to as you can see on row 3 that the amount does not factor in the previous Years ai column when I sum, due to the update takes place at runtime.

## How To Create Total Time, Count It

Aug 29, 2005

i hv a problem in sql,

i) if in data hv time_start, time_end, date , id, how i can calculate the total time for very sesion user login

ii) how to make it if the total come out hv same id(may be two id hv two diffrent total), i want make it just one id then the total time...

post me at jeffliew2001@yahoo.com

## Select TOP 1 And Count Total Records At Same Time

Jun 7, 2012

I have a Select TOP 1 statement which works fine, but i also want to count the total records at the same time. This is my SQL

Code:
SELECT TOP (1) dbo.ClientOffers.ProductID, dbo.ClientOffers.ClientID, dbo.Offers.Offertext, dbo.Offers.Offerlink, dbo.Offers.Image, dbo.Offers.Cost
FROM dbo.ClientOffers INNER JOIN
dbo.Offers ON dbo.ClientOffers.OfferID = dbo.Offers.OfferID
WHERE (dbo.ClientOffers.ProductID = 1284) AND (dbo.ClientOffers.ClientID = 0)

How do i also count the total records?

## T-SQL (SS2K8) :: Calculating Total Outage Time

Jun 17, 2014

I have a requirement to calculate the total outage time, based on logged fault tickets, of network nodes. Basically, multiple tickets may be raised for a single node and those tickets could overlap or sequence over a given period; the task here is to calculate the total time (hh:mm) of the outage in the period.

Ex:

3 tickets raised for a node outage over, say, a 48 hour period. Ticket 1 (spanning a total of 5 hours) overlaps with ticket 2 (spans 3 hours) by 1 hour; ticket 3 starts 5 hours after ticket 2 and spans 1 hour. Total outage time on the tickets is 7hrs + 1hr (T1+T2 minus the 1hr overlap) and the full time of T3.

In summary, it's calculating the total ticket time, allowing for overlaps of tickets, etc.

## T-SQL (SS2K8) :: Finding Total Execution Time?

Oct 30, 2014

I have a SP SPone. i have optimized that and kept it as SPone_Optimized. i would like to test the both SP's execution time to find out how best the optimized one fares.

i planned to test it as follows

declare @starttime datetime,@endtime datetime
declare @count int=0
select @starttime=getdate()
while(@i<10000)
begin
execute SPone_optimized @param='value1'
end
select @endtime=getdate()
select datediff(ms,@stattime,@endtime) 'total_exec_time'

----- for the SP that is before optimize

declare @starttime datetime,@endtime datetime
declare @count int=0
select @starttime=getdate()
while(@i<10000)
begin
execute SPone @param='value1'
end
select @endtime=getdate()
select datediff(ms,@stattime,@endtime) 'total_exec_time'

## Transact SQL :: Total Difference Time In Last Column?

May 23, 2015

I have the below query

DECLARE @GivenDate DATE='2015-05-13'
create table #table (LedgerID int,AttDate Date, checkedtime time,checkedtype varchar(1))
insert into #table (LedgerID,AttDate,checkedtime,checkedtype) values (1232,'2015-05-13','09:01:48.0000000','I')
insert into #table (LedgerID,AttDate,checkedtime,checkedtype) values (1232,'2015-05-13','13:05:52.0000000','O')
insert into #table (LedgerID,AttDate,checkedtime,checkedtype) values (1232,'2015-05-13','14:10:25.0000000','I')

[code]....

the result is like below

i need 'TotalDiffernceTime' column as new column (OUT1-IN1)+(OUT2-IN2).

i am using SQLServer 2008 R2

## Query To Show Total By Shop On Time Range

Aug 28, 2014

How to edit this query to show the total by shop on time range ?

current result.(Time range from 9:00am-23:00pm)
Shop Time_slot cur Amt, yest Amt, Diff Amt, Sales Direction
Abc 10:59 \$100 \$50 +50 (+)
Abc 11:59 \$100 \$50 +150 (+)
Abc 12:59 \$100 \$50 +50 (+)
BBB 11:59 \$100 \$50 +150 (+)
BBB 12:59 \$100 \$50 +50 (+)
------------------------------------------------------------------

Desired Result .
Shop Time_slot cur Amt, yest Amt, Diff Amt, Sales Direction
Abc 10:59 \$100 \$50 +50 (+)
Abc 11:59 \$100 \$50 +150 (+)
Abc 12:59 \$100 \$50 +50 (+)
Total \$300 \$150 +\$200 (+)

BBB 11:59 \$10 \$50 -40 (-)
BBB 12:59 \$10 \$50 -40 (-)
Total \$20 \$100 -80 (-)
-----------------------------------------------------------

select shop
,ltrim(str(datepart(hh,yourdatetimefield)))+':00 - '+ltrim(str(datepart(hh,yourdatetimefield)))+':59' as time_span
,sum(case
when datediff(dd,yourdatetimefield,getdate())=0

[Code] .....

## Transact SQL :: Calculate Total Working Time Against Attendance ID?

May 16, 2015

I have a table like below

i need to calculate total working time against 'AttID' (where I - intime, O - OutTime)

ex :

AttID TotalTime
1 08:02:00
2 07:45:00