question

jsmarlow avatar image
jsmarlow asked

Get prior values with SQL 2008 R2

I have been asked to enhance a listing for a report. The request is to provide prior values for Operation_code, operation_sequencer, and Labor_clockindate. I have tried using sub queries (in the select) for each of these three new columns, it works but takes way too long to run. Is there a better way to get the prior values? The current select is shown below. select jh.partnum,jo.duedate, jo.operation_code,jo.operation.sequencer,jh.jobnum,ld.ClockInDate from jobhead jh inner join joboperation jo on jh.jobnum = jo.jobnum inner join labordetail ld on jo.jobnum = ld.jobnum Now what I need is to get the three values of the prior jo.operation code, sequencer, and ld.clockindate. As mentioned, I tried using subquiries in the select to go after each prior column. It works but it takes way too much time. Thanks for any help.
query-optimisation
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JohnM avatar image JohnM commented ·
Just curious, what's "way too long to run" and how many records are present? There's no WHERE clause so you'll be doing a complete scan of the tables. If there are millions of records, it'll take time. Can you add a WHERE clause to it to help narrow the search?
0 Likes 0 ·

1 Answer

·
jsmarlow avatar image
jsmarlow answered
Hi John. Thanks for responding. Sorry, new to this site, I may have left off the where clause. I redacted a little too much. I will post the entire script below in showing what works but way too slow. By slow, I had to kill the search after 20 minutes. use REP_Reporting select jh.partnum,jo.duedate, jo.oprseq,jo.opcode,jh.plant,jh.jobnum,jo.qtycompleted,jo.runqty,jo.estprodhours,jo.actprodhours,ld.ClockInDate as 'CurrClockIn', ---below is to get the prior job operation key (select Max(joo.OprSeq) from V_JobOper joo where joo.JobNum = jo.JobNum and joo.OprSeq < jo.OprSeq and joo.DueDate between '10/10/2015' and '10/31/2015') as 'Prior Seq', ---below is to get the prior Operation Code (select Distinct(jooo.opcode) from V_JobOper jooo where jooo.OprSeq = (select Max(joooo.OprSeq) from V_JobOper joooo where joooo.OprSeq < jo.Oprseq and joooo.JobNum = jo.JobNum and joooo.DueDate between '10/10/2015' and '10/31/2015')and jooo.JobNum = jo.JobNum) as 'Prior Operation', ---below is to get the prior clockin Date (Select MAX(ldd.ClockInDate) from V_LaborDtl ldd where ldd.JobNum = jo.jobnum and ldd.OprSeq = (select Max(jooooo.OprSeq) from V_JobOper jooooo where jooooo.OprSeq < jo.Oprseq and jooooo.JobNum = jo.JobNum and jooooo.DueDate between '10/10/2015' and '10/31/2015' )and ld.JobNum = jo.JobNum and ldd.LaborType = 'P') as 'PriorClockIn' from v_jobhead jh inner join V_JobProd jp on jh.company = jp.company and jh.JobNum = jp.JobNum inner join v_joboper jo on jh.company = jo.company and jh.jobnum = jo.jobnum inner join V_LaborDtl ld on jo.company = ld.company and jo.jobnum = ld.jobnum and jo.oprseq = ld.OprSeq and jo.OpCode = ld.OpCode where jo.DueDate between '10/10/2015' and '10/31/2015' order by jh.jobnum
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

JohnM avatar image JohnM commented ·
How many records are we talking about here?
0 Likes 0 ·
jsmarlow avatar image jsmarlow commented ·
the rows depends on the table, but in general about 500K rows.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.