Reputation: 401
Hi below is a small sample, I am trying to find a way to reset a COUNT based on ATTENDED_OR_DID_NOT_ATTEND by CDO_OP_REFERRAL_UNIQUE_ID, The final column is the expected outcome which I cant seem to apply in a query, each time I use ROW_NUMBER over partition I keep getting the wrong results in the it doesn't restart again each time the ATTENDED_OR_DID_NOT_ATTEND changes
DECLARE @CDO_OP_APPOINTMENT TABLE (CDO_OP_REFERRAL_UNIQUE_ID int,LOCAL_PATIENT_NUMBER varchar(10),APPOINTMENT_START_DATE_TIME datetime,ATTENDED_OR_DID_NOT_ATTEND varchar(10),DESIRED_OUTCOME varchar(10))
INSERT INTO @CDO_OP_APPOINTMENT
VALUES
('480805568', 'HEY1030785', '05/11/2013 10:00', '2', '1'),
('480805568', 'HEY1030785', '12/11/2013 10:00', '5', '1'),
('480805568', 'HEY1030785', '22/11/2013 09:30', '5', '2'),
('480805568', 'HEY1030785', '03/12/2013 13:00', '3', '1'),
('480805568', 'HEY1030785', '30/12/2013 10:15', '5', '1'),
('480805568', 'HEY1030785', '24/02/2014 09:15', '4', '1'),
('480805568', 'HEY1030785', '24/02/2014 14:15', '5', '1'),
('480805568', 'HEY1030785', '17/03/2014 15:25', '4', '1'),
('480805568', 'HEY1030785', '20/03/2014 18:50', '5', '1'),
('480805568', 'HEY1030785', '23/09/2014 15:55', '5', '2'),
('480805568', 'HEY1030785', '14/04/2015 16:30', '5', '3'),
('480805568', 'HEY1030785', '14/04/2015 17:30', '4', '1'),
('480805568', 'HEY1030785', '15/05/2015 14:15', '5', '1')
SELECT * from @CDO_OP_APPOINTMENT
Upvotes: 0
Views: 502
Reputation: 1270021
This is a gaps-and-islands problem. The simplest solution is a difference of row numbers:
select a.*,
row_number() over (partition by CDO_OP_REFERRAL_UNIQUE_ID, ATTENDED_OR_DID_NOT_ATTEND , seqnum - seqnum_2 order by APPOINTMENT_START_DATE_TIME) as desired_result
from (select a.*,
row_number() over (partition by CDO_OP_REFERRAL_UNIQUE_ID order by APPOINTMENT_START_DATE_TIME) as seqnum,
row_number() over (partition by CDO_OP_REFERRAL_UNIQUE_ID, ATTENDED_OR_DID_NOT_ATTEND order by APPOINTMENT_START_DATE_TIME) as seqnum_2,
from @CDO_OP_APPOINTMENT a
) a;
Upvotes: 1
Reputation: 401
Managed to resolve very similar to answer above
set dateformat dmy
go
DECLARE @CDO_OP_APPOINTMENT TABLE (CDO_OP_REFERRAL_UNIQUE_ID int,LOCAL_PATIENT_NUMBER varchar(10),APPOINTMENT_START_DATE_TIME datetime,ATTENDED_OR_DID_NOT_ATTEND varchar(10),DESIRED_OUTCOME varchar(10))
INSERT INTO @CDO_OP_APPOINTMENT
VALUES
('480805568', 'HEY1030785', '05/11/2013 10:00', '2', '1'),
('480805568', 'HEY1030785', '12/11/2013 10:00', '5', '1'),
('480805568', 'HEY1030785', '22/11/2013 09:30', '5', '2'),
('480805568', 'HEY1030785', '03/12/2013 13:00', '3', '1'),
('480805568', 'HEY1030785', '30/12/2013 10:15', '5', '1'),
('480805568', 'HEY1030785', '24/02/2014 09:15', '4', '1'),
('480805568', 'HEY1030785', '24/02/2014 14:15', '5', '1'),
('480805568', 'HEY1030785', '17/03/2014 15:25', '4', '1'),
('480805568', 'HEY1030785', '20/03/2014 18:50', '5', '1'),
('480805568', 'HEY1030785', '23/09/2014 15:55', '5', '2'),
('480805568', 'HEY1030785', '14/04/2015 16:30', '5', '3'),
('480805568', 'HEY1030785', '14/04/2015 17:30', '4', '1'),
('480805568', 'HEY1030785', '15/05/2015 14:15', '5', '1')
SELECT CDO_OP_REFERRAL_UNIQUE_ID ,LOCAL_PATIENT_NUMBER ,APPOINTMENT_START_DATE_TIME ,ATTENDED_OR_DID_NOT_ATTEND
,ROW_NUMBER() OVER (PARTITION BY CDO_OP_REFERRAL_UNIQUE_ID,Seq1-Seq2,ATTENDED_OR_DID_NOT_ATTEND ORDER BY APPOINTMENT_START_DATE_TIME) AS DESIRED_OUTCOME
FROM
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY CDO_OP_REFERRAL_UNIQUE_ID ORDER BY APPOINTMENT_START_DATE_TIME) AS Seq1,
ROW_NUMBER() OVER (PARTITION BY CDO_OP_REFERRAL_UNIQUE_ID,ATTENDED_OR_DID_NOT_ATTEND ORDER BY APPOINTMENT_START_DATE_TIME) AS Seq2
from @CDO_OP_APPOINTMENT
)t
ORDER BY APPOINTMENT_START_DATE_TIME
Upvotes: 1