Hi
I don't have much experience with programming but I amm trying to build a MsAccess database to help with my work.
The problem I have is that, based on some parameters, I need to merge some records in a table. For example:
DATE | WorkOrder | StartTime | FinishTime
01/01/2000 | 100 | 08:00 | 10:00
01/01/2000 | 100 | 10:45 | 12:00
01/01/2000 ! 100 ! 17:30 | 19:00
What happens is, for the same work order, in the same date, if the time off between 2 shifts is smaller than 4 hours, I need to merge both records using the StartTime of the first record and the FinishTime of the next record as the new start and finish times as below:
DATE | WorkOrder | StartTime | FinishTime
01/01/2000 | 100 | 08:00 | 12:00
01/01/2000 ! 100 ! 17:30 | 19:00
Is it possible to be done on MsAccess? How?
Thanks for your help
Adriano
You can either just loop the records of a recordset and check finish and start times; or you can use this fancy query having two subqueries: - SELECT
-
W.Date,
-
W.WorkOrder,
-
MIN(W.StartTime) As BeginTime,
-
W.EndTime
-
FROM
-
(SELECT
-
WorkOrderTime.Date,
-
WorkOrderTime.WorkOrder,
-
WorkOrderTime.StartTime,
-
(Select
-
Max(T.FinishTime)
-
From [WorkOrderTime] As T
-
Where
-
T.Date = [WorkOrderTime].Date And
-
T.WorkOrder = [WorkOrderTime].[WorkOrder] And
-
T.StartTime < DateAdd("h", 4, [WorkOrderTime].[FinishTime])) AS EndTime
-
FROM
-
WorkOrderTime) As W
-
GROUP BY
-
W.Date,
-
W.WorkOrder,
-
W.EndTime
Sample data:
Output: 4 2994
You can either just loop the records of a recordset and check finish and start times; or you can use this fancy query having two subqueries: - SELECT
-
W.Date,
-
W.WorkOrder,
-
MIN(W.StartTime) As BeginTime,
-
W.EndTime
-
FROM
-
(SELECT
-
WorkOrderTime.Date,
-
WorkOrderTime.WorkOrder,
-
WorkOrderTime.StartTime,
-
(Select
-
Max(T.FinishTime)
-
From [WorkOrderTime] As T
-
Where
-
T.Date = [WorkOrderTime].Date And
-
T.WorkOrder = [WorkOrderTime].[WorkOrder] And
-
T.StartTime < DateAdd("h", 4, [WorkOrderTime].[FinishTime])) AS EndTime
-
FROM
-
WorkOrderTime) As W
-
GROUP BY
-
W.Date,
-
W.WorkOrder,
-
W.EndTime
Sample data:
Output:
It Works !!!
Thanks for your help
This isn't quite right btw, if you move up that forth record to 16:30, it should get collapsed but it wouldn't.
Yes, it needs a little adjustment.
Create this query: - SELECT
-
WorkOrderTime.Date,
-
WorkOrderTime.WorkOrder,
-
WorkOrderTime.StartTime,
-
Null As FinishTime
-
FROM
-
WorkOrderTime
-
WHERE
-
((Select Max(T.FinishTime)
-
From WorkOrderTime As T
-
Where
-
T.Date = WorkOrderTime.Date And
-
T.StartTime < WorkOrderTime.StartTime)) Is Null
-
Or
-
((Select Max(T.FinishTime)
-
From WorkOrderTime As T
-
Where
-
T.Date = WorkOrderTime.Date And
-
T.StartTime < WorkOrderTime.StartTime)) < DateAdd("h",-4,[StartTime])
-
-
UNION ALL
-
-
SELECT
-
WorkOrderTime.Date,
-
WorkOrderTime.WorkOrder,
-
Null As StartTime,
-
WorkOrderTime.FinishTime
-
FROM
-
WorkOrderTime
-
WHERE
-
((Select Min(T.StartTime)
-
From WorkOrderTime As T
-
Where
-
T.Date = WorkOrderTime.Date And
-
T.StartTime > WorkOrderTime.StartTime)) Is Null
-
Or
-
((Select Min(T.StartTime)
-
From WorkOrderTime As T
-
Where
-
T.Date = WorkOrderTime.Date And
-
T.StartTime > WorkOrderTime.StartTime)) > DateAdd("h",4,[FinishTime]);
Save it as Q1. Then create a final query: - SELECT
-
Q1.Date,
-
Q1.WorkOrder,
-
Q1.StartTime,
-
(Select Min(T.FinishTime)
-
From Q1 As T
-
Where T.FinishTime > Q1.StartTime) AS EndTime
-
FROM
-
Q1
-
WHERE
-
Q1.StartTime Is Not Null
-
ORDER BY
-
Q1.Date,
-
Q1.WorkOrder,
-
Q1.StartTime;
Output: - Date WorkOrder StartTime EndTime
-
2020-01-01 100 08:00:00 13:00:00
-
2020-01-01 100 17:30:00 19:00:00
-
2020-01-01 100 23:15:00 23:45:00
or, when changing 17.30 to 16.30: - Date WorkOrder StartTime EndTime
-
2020-01-01 100 08:00:00 19:00:00
-
2020-01-01 100 23:15:00 23:45:00
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Elijah Bailey |
last post by:
I want to sort a set of records using STL's sort() function,
but dont see an easy way to do it.
I have a
char *data;
which has size mn bytes where m is size of the record and
n is the...
|
by: P |
last post by:
Hello,
I am having a difficult time updating a record via a stored procedure using
the gridview and sqldatasource. I cannot seem to be able to find a way to set
everything up so that I can pass...
|
by: Oberon |
last post by:
How do I navigate through records using drag 'n' drop controls?
I have an example in C# for Windows forms that relies on
BindingContext but this is not available for ASP.NET. How do I solve
that...
|
by: jmarr02s |
last post by:
Is it possible to limit the number of Subform records using MS Access
2003?
That is, my end users want the capability of entering up to 12 records
on their subform.
Is that possible?
...
|
by: tushar jadhav |
last post by:
Suppose,
i hv an 10 columns and next to that other columns in a DB Table. That 10 columns hv same default values & others are retriving from other table and are nearly 1000 records. How can i insert...
|
by: sushant2009 |
last post by:
Hi ,
I want to display empty databae table(actually table contains lot of records) to datagrid using dataset and dataadpter.
After displaying i will fill new records in datagrid and i will save...
|
by: Jeffrey Davis |
last post by:
I'm hoping that someone here can give me some assistance with a
database I'm trying to set up. My skills in Access are fairly basic,
and I'm trying to skill up, but some of the stuff is a little...
|
by: ramprakashjava |
last post by:
hi,
i hav "java.lang.NullPointerException" error while Deleting table records using checkbox in jsp here i enclosed files help quickly plzzz..
...
|
by: DCGordon1991 |
last post by:
I am able to run a simple query using criteria for a particular field in one table. Other tables also have this field present. When I try to run a query in these other tables using criteria for this...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |