Compare date in current record with date in previous record

Hi Experts,

I have an SQL table as follows:

Ord #      Item #      Dept      Step      Step_Completion_Date
123         ABC          Packing  1             20041115
123         ABC          QC         2             20041120
123         ABC          QA         3             20041125
456         DEF          Filling     10           20041116
456         DEF          QC         20           20041117
456         DEF          QA         30           20041121

I am looking for the difference between the date field in the current record and date field in the previous record so I can measure 'turnaround' time for the departments. I have been trying various subselect statements but cannot figure it out.

Ideally I would like a view that contained:
Ord#, Item#, Dept, Step_compl_date, Previous_step_comp_date.

Any help greatly appreciated.

TIA
Tom
tomasdebrunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

muzzy2003Commented:
Something like this might do what you want. You will need to fix the columns in the subquery's join to the main query - don't know if you'd want to include [Item#] as well.

SELECT c.[Ord#], c.[Item#], c.Dept, c.Step_compl_date,
(SELECT MAX(Step_compl_date) FROM Table WHERE [Ord#] = c.[Ord#] AND Step_compl_date < c.Step_compl_date) Previous_step_compl_date
FROM Table
0
muzzy2003Commented:
Sorry - typo:

SELECT c.[Ord#], c.[Item#], c.Dept, c.Step_compl_date,
(SELECT MAX(Step_compl_date) FROM Table WHERE [Ord#] = c.[Ord#] AND Step_compl_date < c.Step_compl_date) Previous_step_compl_date
FROM Table c
0
KarinLoosCommented:

select y.ord, y.item, y.dept, y.Step_completion_date, z.Step_completion_date as prev_completion_date
from (
select x.ord, x.Item, x.Dept, x.Step_Completion_date, max(x.Step) as Step
from (
select a.Ord, b.Item, b.Dept, b.Step_completion_date , c.Step
from ( select distinct ord, max(step) as step from #sometable group by ord ) as a
join #sometable b on (b.ord = a.ord and b.step = a.step )
join #sometable c on (c.ord = a.ord and c.Step < a.Step)
) as x
group by x.ord, x.item, x.dept, x.Step_Completion_date
)  as y
join #sometable  z on (z.ord = y.ord and z.Step = y.Step)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

tomasdebrunAuthor Commented:
Thanks Muzzy2003 for your help, the select statement worked a charm for me, the only issue i found with the statement occured when two consecutive steps had the same completion date. The value returned was NULL,I resolved this using the 'Step' field in the statement as the preceding step is alway less then the current step i.e.

SELECT c.[Ord#], c.[Item#], c.Dept, c.Step_compl_date,
(SELECT MAX(Step_compl_date) FROM Table WHERE [Ord#] = c.[Ord#] AND Step< c.Step) Previous_step_compl_date
FROM Table c

I also tried KarinLoos suggestion, unfortunately I kept getting error 'Views or Functions are not allowed on temporary tables'.

Kind Rgds,
Tom
0
BillAn1Commented:
if you want to line a record up with it's previous record try this :


select * from #MyTable curr left outer join #MyTable prev
on curr.ord = prev.ord
and curr.item = prev.item
and prev.step = (select max(step) from #MyTable where ord = curr.ord and item = curr.item and step < curr.step)

if you only want the time stamp, you can get it somewhat simpler,

select *,
(select Step_Completion_date from #MyTable Prev where curr.ord = prev.ord
and curr.item = prev.item and  prev.step = (select max(step) from #MyTable where ord = curr.ord and item = curr.item and step < curr.step))
from #MYTable curr
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tomasdebrunAuthor Commented:
This really is excellent stuff, I have a comprehensive list now to work with, just a small digression regarding BillAn1's 2nd suggestion, the statement needs an AS XYZ added to it i.e.

select *,
(select Step_Completion_date from #MyTable Prev where curr.ord = prev.ord
and curr.item = prev.item and  prev.step = (select max(step) from #MyTable where ord = curr.ord and item = curr.item and step < curr.step))

AS XYZ

from #MYTable curr

thank you all for your help, I have decided to split the points between BillAn1 and Muzzy2003 I hope this is appropriate

Thanks
Tom
 
0
sparabCommented:
Good one
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.