Solved

Close from next field

Posted on 2013-06-03
5
334 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
  • 2
  • 2
5 Comments
 
LVL 77

Expert Comment

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

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
Comment Utility
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
Comment Utility
"Since you want more than one field from the "next record"...?

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

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now