Solved

Close from next field

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

777 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