I am using Microsoft Access 2016 and I have a table where the goal is using the provided start time for each date, add the given Slot to the Start to calculate the End time. Where I am having difficulties is where Start is null, it is to pull the previous records' End time as its Start time and repeat the process.
Below is a sample table of data:
Day
PrimaryKey
AbovePrimaryKey
Start
Slot
End
9/19/2022
171
4:00:00 PM
2:30
9/19/2022
172
171
2:30
9/19/2022
173
172
1:30
9/20/2022
174
173
11:00:00 AM
1:30
9/20/2022
175
174
1:30
9/20/2022
176
175
2:30
9/20/2022
177
176
2:30
9/20/2022
178
177
1:00
9/20/2022
179
178
1:00
Below is what I am wanting as the result:
Day
PrimaryKey
Start
Slot
End
9/19/2022
171
4:00:00 PM
2:30
6:30:00 PM
9/19/2022
172
6:30:00 PM
2:30
8:30:00 PM
9/19/2022
173
8:30:00 PM
1:30
10:00:00 PM
9/20/2022
174
11:00:00 AM
1:30
12:30:00 PM
9/20/2022
175
12:30:00 PM
1:30
2:00:00 PM
9/20/2022
176
2:00:00 PM
2:30
4:30:00 PM
9/20/2022
177
4:30:00 PM
2:30
7:00:00 PM
9/20/2022
178
7:00:00 PM
1:00
8:00:00 PM
9/20/2022
179
8:00:00 PM
1:00
9:00:00 PM
I was able to get the previous row's primary key to use in reference in some subquery to pull the previous row's End, but am unable to determine how to do so, especially since I have also have to calculate Start and End.
Any help would be greatly appreciated. Thank you.
You can do this with four sets of subqueries:
SELECT
SampleData.Day,
SampleData.PrimaryKey,
(Select S2.Start
From SampleData As S2
Where S2.PrimaryKey =
(Select Max(S.PrimaryKey)
From SampleData As S
Where S.Start Is Not Null And S.PrimaryKey <= SampleData.PrimaryKey)) +
(Select CDate(IIf(Sum(S3.Slot) Is Null, #00:00#, Sum(S3.Slot)))
From SampleData As S3
Where S3.PrimaryKey < SampleData.PrimaryKey And S3.PrimaryKey >=
(Select Max(S.PrimaryKey)
From SampleData As S
Where S.Start Is Not Null and S.PrimaryKey <= SampleData.PrimaryKey)) As [Start],
SampleData.Slot,
(Select S2.Start
From SampleData As S2
Where S2.PrimaryKey =
(Select Max(S.PrimaryKey)
From SampleData As S
Where S.Start Is Not Null And S.PrimaryKey <= SampleData.PrimaryKey)) +
(Select CDate(Sum(S3.Slot))
From SampleData As S3
Where S3.PrimaryKey <= SampleData.PrimaryKey And S3.PrimaryKey >=
(Select Max(S.PrimaryKey)
From SampleData As S
Where S.Start Is Not Null And S.PrimaryKey <= SampleData.PrimaryKey)) As [End]
FROM
SampleData;
Output:
Related
I have a timecard table like below. The HourType field represents whether the Employee entered the time ("0"), or whether their supervisor overrides or enters time ("1"). If a supervisor overrides time there is both a 0 record and a 1 record (4/8/2019). If a supervisor enters time the employee did not, then there is only a 1 record (4/12/2019). I need a query to find the total time for a given week. So in the below example the total hours for this week should be 39.5 hours.
RecordId EmployeeId StartDate EndDate StartTime EndTime HourCount HourType
1 100 4/8/2019 4/8/2019 9:00:00 AM 5:00:00 PM 8 0
2 100 4/8/2019 4/8/2019 9:00:00 AM 4:30:00 PM 7.5 1
3 100 4/9/2019 4/9/2019 9:00:00 AM 5:00:00 PM 8 0
4 100 4/10/2019 4/10/2019 9:00:00 AM 5:00:00 PM 8 0
5 100 4/11/2019 4/11/2019 9:00:00 AM 5:00:00 PM 8 0
6 100 4/12/2019 4/12/2019 9:00:00 AM 5:00:00 PM 8 1
Added 2nd data set that would include multiple records per day to account for lunch hours (4/11/2019). Total Hours for this dataset should be 37.5.
RecordId EmployeeId StartDate EndDate StartTime EndTime HourCount HourType
1 100 4/8/2019 4/8/2019 9:00:00 AM 5:00:00 PM 8 0
2 100 4/8/2019 4/8/2019 9:00:00 AM 4:30:00 PM 7.5 1
3 100 4/9/2019 4/9/2019 9:00:00 AM 5:00:00 PM 8 0
4 100 4/10/2019 4/10/2019 9:00:00 AM 5:00:00 PM 8 0
5 100 4/11/2019 4/11/2019 9:00:00 AM 12:00:00 PM 3 0
6 100 4/11/2019 4/11/2019 9:30:00 AM 12:00:00 PM 2.5 1
7 100 4/11/2019 4/11/2019 1:00:00 PM 5:00:00 PM 4 0
8 100 4/12/2019 4/12/2019 9:00:00 AM 5:00:00 PM 8 1
You seem to want:
select employeeId, sum(hourcount)
from (select t.*,
row_number() over (partition by employeeid, startdate, enddate order by hourtype desc) as seqnum
from t
) t
where seqnum = 1;
That is, if multiple records match emplyeeid, startdate, and enddate, then choose the override one.
I have the following table with start and end dates:
dataid TS EndTS
1744 7/27/17 1:57:34 PM 7/27/2017 1:57:38 PM
1743 7/27/17 1:57:31 PM 7/27/2017 1:57:34 PM
1742 7/27/17 1:57:23 PM 7/27/2017 1:57:31 PM
1741 7/27/17 1:57:16 PM 7/27/2017 1:57:23 PM
1740 7/27/17 1:57:04 PM 7/27/2017 1:57:16 PM
1739 7/27/17 1:56:57 PM 7/27/2017 1:57:04 PM
1738 7/27/17 1:56:38 PM 7/27/2017 1:56:57 PM
I would like to get the date/time interval (in seconds) and then calculate a running total.
Here is what I have so far:
SELECT
[dataid] AS [dataid]
DateDiff("s", [TS],[EndTS]) AS [durationsec]
DSum("[durationsec]","[HX32]","[dataid] <=" & [dataid]) AS [add]
FROM [HX32];
I think the datediff() funtion is possibly causing formatting problems. With "[durationsec]" I get all nulls as a result, with [durationsec] I get the following results:
durationsec add
4 6896
3 5169
8 13776
7 12047
12 20640
7 12033
19 32642
I also tried cint(DateDiff("s", [TS],[EndTS])) No change.
I also tried passing durationsec to a table and running a seperate query. No change. (Also I would prefer to do this all in one query)
Here are the results I would like to achieve:
dataid TS EndTS durationsec add
1744 7/27/17 1:57:34 PM 7/27/2017 1:57:38 PM 4 60
1743 7/27/17 1:57:31 PM 7/27/2017 1:57:34 PM 3 56
1742 7/27/17 1:57:23 PM 7/27/2017 1:57:31 PM 8 53
1741 7/27/17 1:57:16 PM 7/27/2017 1:57:23 PM 7 45
1740 7/27/17 1:57:04 PM 7/27/2017 1:57:16 PM 12 38
1739 7/27/17 1:56:57 PM 7/27/2017 1:57:04 PM 7 26
1738 7/27/17 1:56:38 PM 7/27/2017 1:56:57 PM 19 19
Thanks, I'm a beginner.
Time is not seconds but integer days, so you could try:
SELECT
[dataid],
DateDiff("s", [TS], [EndTS]) AS [durationsec],
DSum("[EndTS]-[TS]", "[HX32]", "[dataid] <= " & [dataid] & "") * 86400 AS [add]
FROM
[HX32];
That said, June's method should work as well. If both fail, something else is going on.
SELECT dataid,
startts,
endts,
DATEDIFF("s", startts,endts) AS durationsec,
SUM(DATEDIFF("s", startts,endts)) OVER (ORDER BY endts ROWS UNBOUNDED PRECEDING) AS runningtotal
FROM durtab
ORDER BY 5 DESC;
Result:
dataid startts endts durationsec runningtotal
1744 2017-07-27 13:57:34.000 2017-07-27 13:57:38.000 4 60
1743 2017-07-27 13:57:31.000 2017-07-27 13:57:34.000 3 56
1742 2017-07-27 13:57:23.000 2017-07-27 13:57:31.000 8 53
1741 2017-07-27 13:57:16.000 2017-07-27 13:57:23.000 7 45
1740 2017-07-27 13:57:04.000 2017-07-27 13:57:16.000 12 38
1739 2017-07-27 13:56:57.000 2017-07-27 13:57:04.000 7 26
1738 2017-07-27 13:56:38.000 2017-07-27 13:56:57.000 19 19
DSum is looking at the [HX32] table or query to find a field named [durationsec]. It doesn't exist there.
SELECT
[dataid],
DateDiff("s",[TS],[EndTS]) AS [durationsec],
DSum("DateDiff('s',[TS],[EndTS])","[HX32]","[dataid] <=" & [dataid]) AS [add]
FROM [HX32];`
Note the use of apostrophes to delimit the 's' parameter in the nested DateDiff.
An alternative approach is to do the running sum in Report because textbox in report has a RunningSum property. Domain aggregate functions in queries can perform slowly in large datasets.
I have a table has following data
CREATE TABLE #TempStudentSchedulingRecord(
seq_id int identity(1,1),
Student_ID int ,
PeriodNumber int ,
CPStartTime datetime ,
CPEndTime datetime ,
DateItem datetime ,
FirstSegmentEndTime datetime,
SecondSegmentEndTime datetime
)
Now
Insert Into #TempStudentSchedulingRecord
values(2730,1,'1900-01-01 07:25:00.000','1900-01-01 08:20:00.000','2010-10-05 00:00:00.000','2015-05-27 09:45:00.000','2015-05-27 16:00:00.000')
Insert Into #TempStudentSchedulingRecord values(2730,1,'1900-01-01 08:25:00.000','1900-01-01 10:00:00.000','2010-10-05 00:00:00.000','2015-05-27 09:45:00.000','2015-05-27 16:00:00.000')
Insert Into #TempStudentSchedulingRecord values(2730,1,'1900-01-01 10:05:00.000','1900-01-01 11:35:00.000','2010-10-05 00:00:00.000','2015-05-27 09:45:00.000','2015-05-27 16:00:00.000')
Now Here The firstSegmentTime is same in all rows. I want To find The Total minutes scheduled before First segment End and classes included in first segment.
Similarly find the The Total minutes scheduled between First segment End time and second segment end time and classes included in first segment.
Here the Period 2 resides both segments.. How can I calculate total minutes scheduled before first segment and second segment.
Desired Output is
Student Period CPStartTime CPEndTime DateItem FSET SSET
2730 1 01/01/00 08:25 AM 01/01/00 10:00 AM 10/05/10 12:00 AM 05/27/15 09:45 AM 05/27/15 04:00 PM
2730 2 01/01/00 08:25 AM 05/27/15 09:45 AM 10/05/10 12:00 AM 05/27/15 09:45 AM 05/27/15 04:00 PM
2730 2 05/27/15 09:45 AM 01/01/00 10:00 AM 10/05/10 12:00 AM 05/27/15 09:45 AM 05/27/15 04:00 PM
2730 3 01/01/00 10:05 AM 01/01/00 11:35 AM 10/05/10 12:00 AM 05/27/15 09:45 AM 05/27/15 04:00 PM
I need help for proper Oracle SQL code to combine rows for a crystal reports command object. This is a part of the bigger query I'm working on and got stuck for the past couple of days.
for eg. if the columns are like below
PatId In_time Out_time
151 01/01/2012 07:00:00 am 01/01/2012 10:00:00 am
151 01/01/2012 11:00:00 am 01/02/2012 08:00:00 am
151 01/02/2012 11:00:00 am 01/02/2012 01:00:00 pm
151 01/03/2012 08:00:00 am 01/03/2012 03:00:00 pm
151 01/06/2012 03:30:00 pm 01/09/2012 07:00:00 am
167 01/03/2012 01:30:00 pm 01/09/2012 07:00:00 am
167 01/13/2012 03:30:00 pm 01/14/2012 07:00:00 am
167 01/14/2012 11:30:00 am 01/15/2012 11:30:00 am
167 01/18/2012 12:00:00 pm 01/19/2012 03:00:00 am
Within a PatId, the code should compare the Out_time of one row to the In_time of the next row, and check whether the time gap is greater than 48 hours. If not, then it is considered part of the same visit. I want one result row per PatID & visit, with min(In_time) and max(Out_time). The time span of the visit (result row) itself may be greater than 48 hours.
For this example, for PatId 151 the time difference between the out_time of 1st row and In_time of 2nd row is less than 48 hours. The difference between Out_time of second row and In_time of 3rd row, as well as between the 3rd and 4th rows, is also less than 48 hours. After this the gap between Out_time of the 4th row and In_time of 5th row is greater than 48 hours. The result for PatId 151 should be as below and same for EmpId 167, the chaining should continue until a gap greater than 48 hours is found.
So the result for the above table should be displayed as,
PatId In_time Out_time
151 01/01/2012 07:00:00 am 01/03/2012 03:00:00 pm
151 01/06/2012 03:30:00 pm 01/09/2012 07:00:00 am
167 01/03/2012 01:30:00 pm 01/09/2012 07:00:00 am
167 01/13/2012 03:30:00 pm 01/15/2012 11:30:00 am
167 01/18/2012 12:00:00 pm 01/19/2012 03:00:00 am
I could not get the logic on how to compare and merge rows.
Thanks in Advance, Abhi
General example of subtracting time - copy/paste to see the output. This example will give you differences in hours, minutes, seconds between two dates. The basic formula is (end_date - start_date) * 86400 (number of seconds in 24 hrs)...:
SELECT trunc(mydate / 3600) hr
, trunc(mod(mydate, 3600) / 60) mnt
, trunc(mod(mydate, 3600) / 60 /60) sec
FROM
(
SELECT (to_date('01/03/2012 10:00:00', 'mm/dd/yyyy hh24:mi:ss') -
to_date('01/01/2012 07:00:00', 'mm/dd/yyyy hh24:mi:ss')) * 86400 mydate
FROM dual
)
/
HR | MNT | SEC
---------------
51 | 0 | 0
You need to check your example and logic. I could not understand what needs to be comnpared with what...
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]