Solved

Close from next field

Posted on 2013-06-03
5
352 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 47

Accepted Solution

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

Expert Comment

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

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Creating and Managing Databases with phpMyAdmin in cPanel.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

830 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