Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Close from next field

Posted on 2013-06-03
5
Medium Priority
?
386 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
ID: 39218234
UPDATE table  
SET [Closed] =DMin("[Changed]","table","[JobNo]=" & [JobNo] &
 " and Changed>#" & format(Changed,"yyyy-mm-dd") & "#"))
WHERE [Closed] Is Null;
0
 
LVL 49

Accepted Solution

by:
Dale Fye earned 2000 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 49

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 111

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

971 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