A similar question has been asked before but I've been having trouble adapting those answers to my question.
I want to find rows on consecutive dates, where another column has the same value, and return that as a single date range.
I have tried using OVER
and PARTITION BY
along with ROW_NUMBER() to get the results I need, but I'm not familiar enough with those concepts to figure out the correct output.
The system is Microsoft SQL Server 2014.
Given:
CREATE TABLE EXCEPTIONS
(
ID NUMERIC(18) NOT NULL,
DATE DATE NOT NULL,
TYPE VARCHAR(20) NOT NULL,
VALUE VARCHAR(20),
);
and the data:
INSERT INTO EXCEPTIONS VALUES
(17482, '2016-08-24', 'PRESSURE_EXCEPTION', 'Over'),
(17482, '2016-08-25', 'PRESSURE_EXCEPTION', 'Over'),
(17482, '2016-08-26', 'PRESSURE_EXCEPTION', 'Over'),
(17482, '2016-09-04', 'PRESSURE_EXCEPTION', 'Over'),
(17482, '2016-09-05', 'PRESSURE_EXCEPTION', 'Over'),
(17482, '2016-09-16', 'PRESSURE_EXCEPTION', 'Under'),
(17482, '2016-09-17', 'PRESSURE_EXCEPTION', 'Under'),
(17482, '2016-09-23', 'PRESSURE_EXCEPTION', 'Under'),
(17482, '2016-09-24', 'PRESSURE_EXCEPTION', 'Under'),
(17482, '2016-09-25', 'PRESSURE_EXCEPTION', 'Under'),
(17482, '2016-09-27', 'VOLUME_EXCEPTION', 'Low'),
(17482, '2016-09-28', 'VOLUME_EXCEPTION', 'Low')
The result of this query should be something like
StartDate | EndDate | Type | Value
2016-08-24 | 2016-08-26 | 'PRESSURE_EXCEPTION' | 'Over'
2016-09-04 | 2016-09-05 | 'PRESSURE_EXCEPTION' | 'Over'
2016-09-16 | 2016-09-17 | 'PRESSURE_EXCEPTION' | 'Under'
2016-09-23 | 2016-09-25 | 'PRESSURE_EXCEPTION' | 'Under'
2016-09-27 | 2016-09-28 | 'VOLUME_EXCEPTION' | 'Low'
SQLFiddle available with more data