Solved

Close from next field

Posted on 2013-06-03
5
370 Views
Last Modified: 2013-06-27
Ive been given a table which tell me when a job was moved to a different status, however I need to work out how long it was at each status.

So my table is similar to:-

JobNo      Status            Changed                              By
31181      Status 1            31/01/2013 11:04:44            tcross
31181      Status 2            31/01/2013 11:06:25            tcross
31181      Completed      31/01/2013 14:00:12            tcross

What I want to be able to do is return a table similar to:-

JobNo      Status            Changed                        Closed            Time      Closed      By
31181      Status 1      31/01/2013 11:04:44      31/01/2013 11:06:25      00/00/0000 00:01:41      tcross
31181      Status 2      31/01/2013 11:06:25      31/01/2013 14:00:12      00/00/0000 02:53:47      tcross
31181      Done      31/01/2013 14:00:12            tcross

I was going to use a function to look up the next timedate from the current one and return that, but seems a bit messy.

I was going to look at doing a sub-query but reading up on it it wont work correctly.

Im currently using MSAccess, however it will soon be migrated to MySQL which I think it will be easier to deal with.

Does anyone have any suggestions on what to look at to do this type of query?

Thank you
0
Comment
Question by:tonelm54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 39218234
UPDATE table  
SET [Closed] =DMin("[Changed]","table","[JobNo]=" & [JobNo] &
 " and Changed>#" & format(Changed,"yyyy-mm-dd") & "#"))
WHERE [Closed] Is Null;
0
 
LVL 48

Accepted Solution

by:
Dale Fye earned 500 total points
ID: 39218652
Since you want more than one field from the "next record", I generally do this by starting out with a query that identifies the predecessor and the successor

SELECT T1.JobNo
           , T1.Status
           , T1.Changed As Start
           , Min(T2.Changed) as Closed
FROM yourTable T1
LEFT JOIN yourTable T2
ON T1.JobNo = T2.JobNo
AND T1.Changed < T2.Changed
GROUP By T1.JobNo, T1.Status, T1.Changed

I then use this as a subquery, to join the table to itself based on the JobNo and time, to get the remainder of the data from the Next events record

SELECT T3.JobNo
           , T3.Status
           , T3.Start
           , T3.Closed
           , T4.By as [Closed By]
FROM
(SELECT T1.JobNo
           , T1.Status
           , T1.Changed As Start
           , Min(T2.Changed) as Closed
FROM yourTable T1
LEFT JOIN yourTable T2
ON T1.JobNo = T2.JobNo
AND T1.Changed < T2.Changed
GROUP By T1.JobNo, T1.Status, T1.Changed) as T3
LEFT JOIN yourTable as T4
ON T3.JobNo = T4.JobNo
AND T3.Closed = T4.Changed
0
 
LVL 77

Expert Comment

by:peter57r
ID: 39218662
"Since you want more than one field from the "next record"...?

I think all that is needed is the Change date.
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 39218708
peter,

I got the impression that the "closed by" column was the "by" column from the "next" record, if that was not the OPs intention, then the following should work.  Should also be much faster than using the Domain Function.

SELECT T1.JobNo
           , T1.Status
           , T1.Changed As Start
           , Min(T2.Changed) as Closed
           , T1.By as [Closed By]
FROM yourTable T1
LEFT JOIN yourTable T2
ON T1.JobNo = T2.JobNo
AND T1.Changed < T2.Changed
GROUP By T1.JobNo, T1.Status, T1.Changed, T1.By
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39218797
This date format is going to get you in trouble.

31/01/2013 11:04:44

Instead, choose the ISO-8601 standard like 2013-01-31T11:04:44 This article tells why.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question