473,411 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Merging records using a criteria

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
Oct 1 '20 #1

✓ answered by cactusdata

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:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     W.Date, 
  3.     W.WorkOrder, 
  4.     MIN(W.StartTime) As BeginTime,
  5.     W.EndTime
  6. FROM
  7.     (SELECT 
  8.         WorkOrderTime.Date, 
  9.         WorkOrderTime.WorkOrder, 
  10.         WorkOrderTime.StartTime, 
  11.         (Select 
  12.             Max(T.FinishTime) 
  13.             From [WorkOrderTime] As T 
  14.             Where 
  15.                 T.Date = [WorkOrderTime].Date And 
  16.                 T.WorkOrder = [WorkOrderTime].[WorkOrder] And 
  17.                 T.StartTime < DateAdd("h", 4, [WorkOrderTime].[FinishTime])) AS EndTime
  18.     FROM 
  19.         WorkOrderTime) As W
  20. GROUP BY
  21.     W.Date, 
  22.     W.WorkOrder, 
  23.     W.EndTime
Sample data:



Output:


4 2994
cactusdata
214 Expert 128KB
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:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.     W.Date, 
  3.     W.WorkOrder, 
  4.     MIN(W.StartTime) As BeginTime,
  5.     W.EndTime
  6. FROM
  7.     (SELECT 
  8.         WorkOrderTime.Date, 
  9.         WorkOrderTime.WorkOrder, 
  10.         WorkOrderTime.StartTime, 
  11.         (Select 
  12.             Max(T.FinishTime) 
  13.             From [WorkOrderTime] As T 
  14.             Where 
  15.                 T.Date = [WorkOrderTime].Date And 
  16.                 T.WorkOrder = [WorkOrderTime].[WorkOrder] And 
  17.                 T.StartTime < DateAdd("h", 4, [WorkOrderTime].[FinishTime])) AS EndTime
  18.     FROM 
  19.         WorkOrderTime) As W
  20. GROUP BY
  21.     W.Date, 
  22.     W.WorkOrder, 
  23.     W.EndTime
Sample data:



Output:

Oct 1 '20 #2
It Works !!!

Thanks for your help
Oct 1 '20 #3
Rabbit
12,516 Expert Mod 8TB
This isn't quite right btw, if you move up that forth record to 16:30, it should get collapsed but it wouldn't.
Oct 1 '20 #4
cactusdata
214 Expert 128KB
Yes, it needs a little adjustment.

Create this query:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     WorkOrderTime.Date, 
  3.     WorkOrderTime.WorkOrder, 
  4.     WorkOrderTime.StartTime, 
  5.     Null As FinishTime
  6. FROM 
  7.     WorkOrderTime
  8. WHERE 
  9.     ((Select Max(T.FinishTime) 
  10.     From WorkOrderTime As T 
  11.     Where 
  12.         T.Date = WorkOrderTime.Date And 
  13.         T.StartTime < WorkOrderTime.StartTime)) Is Null 
  14.         Or 
  15.         ((Select Max(T.FinishTime) 
  16.         From WorkOrderTime As T 
  17.         Where 
  18.             T.Date = WorkOrderTime.Date And 
  19.             T.StartTime < WorkOrderTime.StartTime)) < DateAdd("h",-4,[StartTime])
  20.  
  21. UNION ALL
  22.  
  23. SELECT 
  24.     WorkOrderTime.Date, 
  25.     WorkOrderTime.WorkOrder, 
  26.     Null As StartTime, 
  27.     WorkOrderTime.FinishTime
  28. FROM 
  29.     WorkOrderTime
  30. WHERE 
  31.     ((Select Min(T.StartTime) 
  32.     From WorkOrderTime As T 
  33.     Where 
  34.         T.Date = WorkOrderTime.Date And 
  35.         T.StartTime > WorkOrderTime.StartTime)) Is Null 
  36.         Or 
  37.         ((Select Min(T.StartTime) 
  38.         From WorkOrderTime As T 
  39.         Where 
  40.             T.Date = WorkOrderTime.Date And 
  41.             T.StartTime > WorkOrderTime.StartTime)) > DateAdd("h",4,[FinishTime]);
Save it as Q1. Then create a final query:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     Q1.Date, 
  3.     Q1.WorkOrder, 
  4.     Q1.StartTime, 
  5.     (Select Min(T.FinishTime) 
  6.     From Q1 As T 
  7.     Where T.FinishTime > Q1.StartTime) AS EndTime
  8. FROM 
  9.     Q1
  10. WHERE 
  11.     Q1.StartTime Is Not Null
  12. ORDER BY 
  13.     Q1.Date, 
  14.     Q1.WorkOrder, 
  15.     Q1.StartTime;
Output:

Expand|Select|Wrap|Line Numbers
  1. Date    WorkOrder    StartTime    EndTime
  2. 2020-01-01    100    08:00:00    13:00:00
  3. 2020-01-01    100    17:30:00    19:00:00
  4. 2020-01-01    100    23:15:00    23:45:00
or, when changing 17.30 to 16.30:

Expand|Select|Wrap|Line Numbers
  1. Date    WorkOrder    StartTime    EndTime
  2. 2020-01-01    100    08:00:00    19:00:00
  3. 2020-01-01    100    23:15:00    23:45:00
Oct 1 '20 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

40
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...
1
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...
0
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...
1
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? ...
3
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...
1
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...
4
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...
13
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.. ...
1
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...
0
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
0
BarryA
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...
1
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...
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
agi2029
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,...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.