i want to split the time and calculate time difference using sql server 2005
my default output is like this:
EnrollNo AttDateFirst AttDateLast
111 2011-12-09 08:46:00.000 2011-12-09 08:46:00.000
112 2011-12-09 08:40:00.000 2011-12-09 17:30:00.000
302 2011-12-09 09:00:00.000 2011-12-09 18:30:00.000
303 2011-12-09 10:00:00.000 2011-12-09 18:35:00.000
I want my new output to be like this:
Enroll No ..... FirtTime LastTime Time Diff
111 ..... 8:46:00 8:45:00 00:00:00
112 ..... 8:30:00 17:30:00 9:00:00
302 ..... 9:00:00 18:30:00 9:30:00
303 ..... 10:00:00 18:35:00 8:35:00
You can use this query:
select EnrollNo, convert(varchar, AttDateFirst, 8) as FirstTime,
convert(varchar, AttDateLast, 8) as LastTime,
convert(varchar, AttDateLast - AttDateFirst, 8) as [Time Diff]
from YourTable
to return the following results:
EnrollNo FirstTime LastTime Time Diff
----------- ------------------------------ ------------------------------ ------------------------------
111 08:46:00 08:46:00 00:00:00
112 08:30:00 17:30:00 09:00:00
302 09:00:00 18:30:00 09:30:00
303 10:00:00 18:35:00 08:35:00
you can use
select DATEDIFF(day,2007-11-30,2007-11-20) AS NumberOfDays,
DATEDIFF(hour,2007-11-30,2007-11-20) AS NumberOfHours,
DATEDIFF(minute,2007-11-30,2007-11-20) AS NumberOfMinutes from
test_table
to split u can use
substring(AttDateFirst,charindex(' ',AttDateFirst)+1 ,
len(AttDateFirst)) as [FirstTime]
Related
I have a dataframe like this:
df11 = pd.DataFrame(
{
"Start_date": ["2018-01-31 12:00:00", "2018-02-28 16:00:00", "2018-02-27 22:00:00"],
"End_date": ["2019-01-31 21:45:00", "2019-03-24 22:00:00", "2018-02-28 01:00:00"],
}
)
Start_date End_date
0 2018-01-31 12:00:00 2019-01-31 21:45:00
1 2018-02-28 16:00:00 2019-03-24 22:00:00
2 2018-02-27 22:00:00 2018-02-28 01:00:00
I need to check the overlap time duration in specific periods in seconds. My expected results are like this:
Start_date End_date 12h-16h 16h-22h 22h-00h 00h-02h30
0 2018-01-31 12:00:00 2019-01-31 21:45:00 14400 20700 0 0
1 2018-02-28 16:00:00 2019-03-24 22:00:00 0 21600 0 0
2 2018-02-27 22:00:00 2018-02-28 01:00:00 0 0 7200 3600
I know it`s completely wrong and I´ve tried other solutions. This is one of my attempts:
df11['12h-16h']=np.where(df11['Start_date']<timedelta(hours=16, minutes=0, seconds=0) & df11['End_date']>timedelta(hours=12, minutes=0, seconds=0),(np.minimum(df11['End_date'],timedelta(hours=16, minutes=0, seconds=0)))-(np.maximum(df11['Start_date'],timedelta(hours=12, minutes=0, seconds=0)))
So I have a command that looks like this:
SELECT
UserID,
FacilityMMXID,
ScheduleDate,
StartTime,
EndTime
FROM TblPASchedule
WHERE UserID = 244 AND MONTH(ScheduleDate) = 03 AND Year(ScheduleDate) = 2017
The output looks like this
UserID FacilityMMXID ScheduleDate StartTime EndTime
----------- ------------- ------------ ---------------- ----------------
244 1 2017-03-17 01:00:00 05:00:00
244 2 2017-03-17 01:00:00 05:00:00
244 3 2017-03-17 01:00:00 05:00:00
244 4 2017-03-17 01:00:00 05:00:00
244 5 2017-03-17 01:00:00 05:00:00
244 6 2017-03-17 01:00:00 05:00:00
244 7 2017-03-17 01:00:00 05:00:00
244 8 2017-03-17 01:00:00 05:00:00
244 9 2017-03-17 01:00:00 05:00:00
244 10 2017-03-17 01:00:00 05:00:00
244 11 2017-03-17 01:00:00 05:00:00
244 12 2017-03-17 01:00:00 05:00:00
244 13 2017-03-17 01:00:00 05:00:00
244 14 2017-03-17 01:00:00 05:00:00
244 15 2017-03-17 01:00:00 05:00:00
244 1 2017-03-17 05:00:00 22:00:00
244 2 2017-03-17 05:00:00 22:00:00
244 3 2017-03-17 05:00:00 22:00:00
244 4 2017-03-17 05:00:00 22:00:00
244 5 2017-03-17 05:00:00 22:00:00
244 6 2017-03-17 05:00:00 22:00:00
244 7 2017-03-17 05:00:00 22:00:00
244 8 2017-03-17 05:00:00 22:00:00
244 9 2017-03-17 05:00:00 22:00:00
244 10 2017-03-17 05:00:00 22:00:00
244 11 2017-03-17 05:00:00 22:00:00
244 12 2017-03-17 05:00:00 22:00:00
244 13 2017-03-17 05:00:00 22:00:00
244 14 2017-03-17 05:00:00 22:00:00
244 15 2017-03-17 05:00:00 22:00:00
I left out the ID row as it really isn't important in this case.
Also- yes- I realize that this table is very very redundant- It isn't something I can currently fix as I am not allowed to- I can only work on getting the aforementioned summing function working.
The end goal is to pair off the distinct StartTime and EndTime pairs and then find the date difference of those- and then, for the entire month- find the sum of all the entries.
This is as far as I have gotten:
Using:
SELECT
UserID,
DATEDIFF(HOUR, StartTime, EndTime) AS 'Hours Worked'
FROM TblPASchedule WHERE UserID = 244 AND MONTH(ScheduleDate) = 03 AND Year(ScheduleDate) = 2017
GROUP BY UserId, StartTime, EndTime
I get the output to be:
UserID Hours Worked
----------- ------------
244 4
244 17
But I am not too sure about where I should go from here.
I eventually need to make it group these sums based on the UserIDs, but one step at a time I suppose. I am using a where clause to work with a single id for now...
This query gets all the distinct sets of UserID, Starttime and Endtime
;WITH CTE AS
(SELECT DISTINCT UserID, StartTime, EndTime FROM [dbo].[TblPASchedule])
SELECT SUM(DATEDIFF(MINUTE, StartTime, EndTime))/60.0 AS 'Hours Worked', UserID
FROM CTE GROUP BY UserID
RESULTS look like this
Hours Worked UserID
1.666666 19
1.233333 37
0.500000 38
Have you tried wrapping additional sub query on top of your groups?
SELECT UserId, SUM('Hours Worked') as 'Hours Worked' FROM (
SELECT
UserID,
DATEDIFF(HOUR, StartTime, EndTime) AS 'Hours Worked'
FROM TblPASchedule WHERE UserID = 244 AND MONTH(ScheduleDate) = 03 AND Year(ScheduleDate) = 2017
GROUP BY UserId, StartTime, EndTime
) AS temp
GROUP BY UserId
am using excel sheet to display data from sql with this query
SELECT itable.Timestamp, itable.Time,
Sum(itable.CallsOffered)AS CallsOffered, Sum(itable.CallsAnswered)AS CallsAnswered, Sum(itable.CallsAnsweredAftThreshold)AS CallsAnsweredAftThreshold,
sum(CallsAnsweredDelay)AS CallsAnsweredDelay
FROM tablename itable
WHERE
(itable.Timestamp>=?) AND (itable.Timestamp<=?) AND
(itable.Application in ('1','2','3','4'))
GROUP BY itable.Timestamp, itable.Time
ORDER BY itable.Timestamp, itable.Time
and i get a data with an interval of 15 minutes like this :
Timestamp Time CallsOffered CallsAnswered CallsAnsweredAftThreshold CallsAnsweredDelay
6/1/2014 0:00 00:00 0 1 1 52
6/1/2014 0:15 00:15 3 1 1 23
6/1/2014 0:30 00:30 3 3 2 89
6/1/2014 0:45 00:45 0 0 0 0
6/1/2014 1:00 01:00 0 0 0 0
6/1/2014 1:15 01:15 4 1 1 12
6/1/2014 1:30 01:30 1 1 1 39
6/1/2014 1:45 01:45 0 0 0 0
6/1/2014 2:00 02:00 2 1 0 7
6/1/2014 2:15 02:15 1 1 1 80
6/1/2014 2:30 02:30 3 2 2 75
6/1/2014 2:45 02:45 0 0 0 0
6/1/2014 3:00 03:00 0 0 0 0
and i want to convert the interval from being 15 minutes to hourly interval
like this
2014-07-01 00:00:00.000
2014-07-01 01:00:00.000
2014-07-01 02:00:00.000
2014-07-01 03:00:00.000
2014-07-01 04:00:00.000
2014-07-01 05:00:00.000
2014-07-01 06:00:00.000
2014-07-01 07:00:00.000
2014-07-01 08:00:00.000
2014-07-01 09:00:00.000
2014-07-01 10:00:00.000
2014-07-01 11:00:00.000
2014-07-01 12:00:00.000
2014-07-01 13:00:00.000
2014-07-01 14:00:00.000
the query i came up with is :
select
timestamp = DATEADD(hour,datediff(hour,0,app.Timestamp),0),
Sum(app.CallsOffered)AS CallsOffered,
Sum(app.CallsAnswered)AS CallsAnswered,
Sum(app.CallsAnsweredAftThreshold)AS CallsAnsweredAftThreshold,
sum(CallsAnsweredDelay)AS CallsAnsweredDelay,
max(MaxCallsAnsDelay) as MaxCallsAnsDelay ,
max(app.MaxCallsAbandonedDelay)as MaxCallsAbandonedDelay
from tablename app
where Timestamp >='2014-7-1' AND timestamp<='2014-7-2' and
(app.Application in (
'1',
'2',
'3',
'4')
group by DATEADD(hour,datediff(hour,0,Timestamp),0)
order by Timestamp;
i get the result i want when i run in in Microsoft Sql server Managment studio
but it gives me a long error when i try running the same query in Microsoft Query in excel the error is like i cant start with timestamp
and that its giving me error for DATEADD ,DATEDIFF
so is there something i should change in my query or anything i can do to get an hourly count interval instead of 15 minutes count interval as ive shown
and thank you in advance
I have problem about combining tables in store procedure.
Note : field "Time" is varchar
First table (tbTime)
Time
08:00:00
08:30:00
09:00:00
09:30:00
10:00:00
10:30:00
11:00:00
11:30:00
12:00:00
12:30:00
13:00:00
13:30:00
14:00:00
14:30:00
15:00:00
15:30:00
16:00:00
16:30:00
17:00:00
17:30:00
18:00:00
18:30:00
19:00:00
19:30:00
20:00:00
Second table (tbClassRsv)
select * from tbclassrsv where transdate='2014-02-05 00:00:00' and status<>'DEL'
transDate time until status studentCode tutor class description userID
2014-02-05 16:48:14 17:48:14 OPN ET-7201 ET-444 ROOM 01 try ADMIN
I want the result with condition schedule like this
Time Student
08:00:00 -
08:30:00 -
09:00:00 -
09:30:00 -
10:00:00 -
10:30:00 -
11:00:00 -
11:30:00 -
12:00:00 -
12:30:00 -
13:00:00 -
13:30:00 -
14:00:00 -
14:30:00 -
15:00:00 -
15:30:00 -
16:00:00 -
16:30:00 ET-7201
17:00:00 ET-7201
17:30:00 ET-7201
18:00:00 ET-7201
18:30:00 -
19:00:00 -
19:30:00 -
20:00:00 -
Thanks for reading or answer ^_^
GBU
I`ve tried this
select t.time,
isnull(
(select c.studentCode
from tbclassrsv c
where c.transdate='2014-02-05 00:00:00'
and c.class='ROOM 01'
and c.status<>'DEL'
and t.time>=c.time
and t.time<=c.until
),'-') [Student]
The result is....
Time Student
08:00:00 -
08:30:00 -
09:00:00 -
09:30:00 -
10:00:00 -
10:30:00 -
11:00:00 -
11:30:00 -
12:00:00 -
12:30:00 -
13:00:00 -
13:30:00 -
14:00:00 -
14:30:00 -
15:00:00 -
15:30:00 -
16:00:00 -
16:30:00 -
17:00:00 ET-7201
17:30:00 ET-7201
18:00:00 -
18:30:00 -
19:00:00 -
19:30:00 -
20:00:00 -
Try this. you were not converting your varchar times to datetime so that your time comparisons would work.
select t.time,
isnull(
(select c.studentCode
from tbClassRsv c
where c.transdate='2014-02-05 00:00:00'
and c.class='ROOM 01'
and c.status<>'DEL'
and DateAdd(MINUTE, 30, Convert(datetime, t.time))>= Convert(datetime, c.time)
and Convert(datetime, t.time) <= Convert(datetime, c.until)
),'-') from [tbTime] t
What you need to do is round c.time down to the nearest 30 minutes interval, and round c.until up to the nearest interval. This way your where clause will have the correct range.
To do the rounding you will need to convert the times to datetime which you can do like so:
CAST(CONVERT(varchar,THE_TIME_AS_VARCHAR,121) AS datetime)
Then you can round down to the nearest 30 minutes like so:
DATEADD(mi, DATEDIFF(mi, 0, THE_TIME_AS_DATETIME)/30*30, 0)
And round up like so:
DATEADD(mi, DATEDIFF(mi, 30, THE_TIME_AS_DATETIME)/30*30, 0)
Applying all that to your existing code would give you this:
select t.time,
isnull(
(select c.studentCode
from tbclassrsv c
where c.transdate='2014-02-05 00:00:00'
and c.class='ROOM 01'
and c.status<>'DEL'
and t.time>= DATEADD(mi, DATEDIFF(mi, 0, CAST(CONVERT(varchar,c.time,121) AS datetime))/30*30, 0)
and t.time<= DATEADD(mi, DATEDIFF(mi, 30, CAST(CONVERT(varchar,c.until,121) AS datetime))/30*30, 0)
),'-') [Student]
Using VB6 and MS Access
Table:
ID Lunch_Intime, Lunch_Outtime
001 13:00:00 14:00:00
002 12:00:00 13:00:00
003 12:00:00 15:00:00
004 14:00:00 16:00:00
So on…
Lunch_Intime, Lunch_Outtime column data type is text.
I want to get a Total_Lunch_Time for the id.
Tried Query:
Select Lunch_Intime,
Lunch_Outtime,
Lunch_Outtime - Lunch_Intime as Total_Lunch_Time
from table
...but it's showing:
Total_Lunch_Time
#error
#error
So on..,
How to make a query for total_Lunch_Time?
Expected Output.
ID Lunch_Intime, Lunch_Outtime Total_Lunch_Time
001 13:00:00 14:00:00 01:00:00
002 12:00:00 13:00:00 01:00:00
003 12:00:00 15:00:00 03:00:00
004 14:00:00 16:00:00 02:00:00
In addition to converting your "time" values from text to date/time, I think you want to apply Format() to the elapsed times.
SELECT
ID
, Lunch_Intime
, Lunch_Outtime
, Format(CDate(Lunch_Outtime) - Cdate(Lunch_Intime),
"hh:nn:ss") AS Total_Lunch_Time
FROM
table;
You must cast the hours fields into date/time using CDate() before subtracting them.