This one fascinated me when I saw it, and I wondered how I would go about solving it. I was too busy to get in with an answer first, and I got it working later but have sat on it for a few days since! After a few days I still understand what I devised, which is a good sign :)
I've added some extra data at the end to demonstrate that this works with single-row JobNumber entries, rather than assuming that weighings will always be in batches, but the first rows in the results match the original solution.
This approach also uses cascading CTEs (one more than the accepted answer here but I won't let that discourage me!) with the first being the test data setup :
With Weighs AS -- sample data
(
SELECT 100 AS JobNumber, '01/01/2014 08:00' AS TimeOfWeigh UNION ALL
SELECT 100 AS JobNumber, '01/01/2014 09:00' AS TimeOfWeigh UNION ALL
SELECT 100 AS JobNumber, '01/01/2014 10:00' AS TimeOfWeigh UNION ALL
SELECT 200 AS JobNumber, '01/01/2014 12:00' AS TimeOfWeigh UNION ALL
SELECT 200 AS JobNumber, '01/01/2014 13:00' AS TimeOfWeigh UNION ALL
SELECT 300 AS JobNumber, '01/01/2014 15:00' AS TimeOfWeigh UNION ALL
SELECT 300 AS JobNumber, '01/01/2014 16:00' AS TimeOfWeigh UNION ALL
SELECT 100 AS JobNumber, '02/01/2014 08:00' AS TimeOfWeigh UNION ALL
SELECT 100 AS JobNumber, '02/01/2014 09:00' AS TimeOfWeigh UNION ALL
SELECT 100 AS JobNumber, '03/01/2014 10:00' AS TimeOfWeigh UNION ALL
SELECT 400 AS JobNumber, '04/01/2014 14:00' AS TimeOfWeigh UNION ALL
SELECT 300 AS JobNumber, '04/01/2014 14:30' AS TimeOfWeigh
)
,
Numbered AS -- add on a unique consecutive row number
( SELECT *, ROW_NUMBER() OVER (ORDER BY TimeOfWeigh) AS ID FROM Weighs )
,
GroupEnds AS -- add on a 1/0 flag for whether it's the first or last in a run
( SELECT *,
CASE WHEN -- next row is different JobNumber?
(SELECT ID FROM Numbered n2 WHERE n2.ID=n1.ID+1 AND n2.JobNumber=n1.JobNumber) IS NULL
THEN 1 ELSE 0 END AS GroupEnd,
CASE WHEN -- previous row is different JobNumber?
(SELECT ID FROM Numbered n2 WHERE n2.ID=n1.ID-1 AND n2.JobNumber=n1.JobNumber) IS NULL
THEN 1 ELSE 0 END AS GroupBegin
FROM Numbered n1
)
,
Begins_and_Ends AS -- make sure there are always matching pairs
( SELECT * FROM GroupEnds WHERE GroupBegin=1
UNION ALL
SELECT * FROM GroupEnds WHERE GroupEnd=1
)
,
Pairs AS -- give matching pairs the same ID number for GROUPing next..
( SELECT *, (1+Row_Number() OVER (ORDER BY ID))/2 AS PairID
FROM Begins_and_Ends
)
SELECT
Min(JobNumber) AS JobNumber,
Min(TimeOfWeigh) as [First Weigh],
Max(TimeOfWeigh) as [Last Weigh]
FROM Pairs
GROUP BY PairID
ORDER BY PairID
The Numbered
CTE is fairly obvious, giving an ordered ID number to each row.
CTE GroupEnds
adds on a pair of booleans - a 1 or 0 if the row is the first or last in a run of JobNumbers - by trying to see if the next or previous row is the same JobNumber.
From there I simply needed a way to pair up the adjacent GroupBegins and GroupEnds. I played with the N-tile ranking function NTILE() to generate these numbers by dividing the rowcount by 2 by counting the GroupEnds and SELECTing that result as the parameter for NTILE() - but this broke when there were an odd number of rows due to single-row batches where the same row is a Begin and End of a batch.
I got around this by guaranteeing an equal number of Begin and End rows : a UNION of Begin rows and End rows, even if some are the same rows. This is CTE Begins_and_Ends
.
The Pairs
CTE adds on Pair Numbers using Row_Number() divided by two - the integer result PairID
being the same for pairs of rows.
This gives us the following - all rows in the middle of JobNumber batches have been filtered out by now :
JOBNUMBER TIMEOFWEIGH ID End? Begin PairID
100 01/01/2014 08:00 1 0 1 1
100 01/01/2014 10:00 3 1 0 1
200 01/01/2014 12:00 4 0 1 2
200 01/01/2014 13:00 5 1 0 2
300 01/01/2014 15:00 6 0 1 3
300 01/01/2014 16:00 7 1 0 3
100 02/01/2014 08:00 8 0 1 4
100 03/01/2014 10:00 10 1 0 4
400 04/01/2014 14:00 11 1 1 5
400 04/01/2014 14:00 11 1 1 5
300 04/01/2014 14:30 12 1 1 6
300 04/01/2014 14:30 12 1 1 6
From there it's now a final piece of cake to GROUP BY the PairID
and grab the first and last weigh times. I enjoyed the challenge, I wonder if anyone else finds it useful in any weigh!
http://sqlfiddle.com/#!3/b4f39/48