We have a nightly job that uses sql loader to insert data from a csv file. Here is a small sample of the data:
select * from ktimov.press_schd order by job;
PLANT JOB EST_COMPLETION INSERT_DATE
10 111 6/26/2017 5/10/2017
10 111 7/1/2017 5/11/2017
10 111 7/5/2017 5/12/2017
10 222 6/20/2017 5/10/2017
10 222 6/26/2017 5/11/2017
10 333 7/26/2017 5/10/2017
10 333 6/30/2017 5/11/2017
10 333 8/10/2017 5/12/2017
10 333 8/6/2017 5/13/2017
10 333 8/7/2017 5/14/2017
INSERT_DATE
is default sysdate of when the record was inserted.
EST_COMPLETION
is an estimate of when the JOB will be completed. So for example:
On 5/10/17, JOB# 111 was estimated to be completed by 6/26. Then on 5/11 the estimated completion was changed to 7/1.
I'm looking to query this data to show the following:
JOB#, First EST Date, Last Est date, Count of how many times it was changed.
The end result should be like this:
JOB FIRST_DATE LAST DATE Count_Changes
111 6/26/2017 7/5/2017 3
222 6/20/2017 6/26/2017 2
333 7/26/2017 8/7/2017 5
Any ideas how I can accomplish this?
Best Answer
Here's a simple method. We use a CTE to get the first and last
INSERT_DATE
s for each job; then, join to the main table to retrieve the estimated completions dates from those first and last loads:NOTE: I'm assuming that the first load is simply establishing the initial values, and that only subsequent loads are counted as changes. If you really want the total number of loads, change
j.load_count - 1
toj.load_count
.