I need to create a query where patients have delivered and may have returned within a certain number of days for another admittance.
each visit a new chart(sessionid) will be created.
I thought I could create a visit id number which would count the number of visits per patient.
The first visit has to be the day of delivery where delivery date is not null.
I could create a
CASE WHEN MT.DeliveryTime IS NOT NULL THEN 1
WHEN MT.ChartStartTime > MT.DeliveryTime THEN 2 END AS VISITID, but that did not work they way i envisioned.
how would i get the next visit date as visit 2????
FacilityID sessionId MRN PATIENT ChartStartTime DeliveryTime VISIT_ID
0 36957 123456 BETTY SUE 7/20/2015 NULL 1
0 37695 123456 BETTY SUE 8/29/2015 NULL 1
0 37824 123456 BETTY SUE 9/5/2015 9/5/2015 1
0 37916 123456 BETTY SUE 9/10/2015 NULL 1
I used the following but it is not working.
ROW_NUMBER() OVER (PARTITION BY S.SESSIONID ORDER BY S.MRN) AS VISIT_ID
I put the following query together but im having a tough time with this.
SELECT
S.FacilityID,
S.sessionId,
S.MRN,
S.LastName + ', ' + S.firstname AS PATIENT,
DATEDIFF(D,MT.DeliveryTime,MT.DISCHARGETIME)AS LENGTH_OF_STAY,
isnull(dbo.EFgetFindingValue(30741,default,S.Sessionid),999) AS GESTATIONAL_AGE,
CASE WHEN DT.VaginalDelivery = 'YES' THEN 'VAGINAL'
WHEN DT.CesareanDelivery = 'YES' THEN 'CESAREAN' END AS DELIVERY_TYPE,
MT.ChartStartTime,
MT.DeliveryTime,
ROW_NUMBER() OVER (PARTITION BY S.SESSIONID ORDER BY S.MRN) AS VISIT_ID
FROM
dbo.BLSession_Extended AS S
LEFT OUTER JOIN
dbo.MO_DeliveryTypePatient_table AS DT ON S.sessionID = DT.SessionID
INNER JOIN
dbo.MO_Times MT ON S.sessionId = MT.SessionID
WHERE --(F.ObjectName = 'Time_Of_Delivery!Delivery_Report_Neonate[#]') AND (F.ValueStr = 'true')
MT.ChartStartTime between '07/01/2015' and '09/30/2015' and S.FacilityID=0
--S.MRN IN ('300002660890','100004902885','300002758666','100004489667','100004939095','100004215165')
--AND s.MRN in
-- (SELECT s_PrevSession.MRN
-- FROM
-- BLSession_Extended s_PrevSession
-- INNER JOIN MO_Times t_PrevSession ON s_PrevSession.sessionID = t_PrevSession.SessionID
-- WHERE
-- s_PrevSession.SessionID <> s.SessionID
-- AND t_PrevSession.DeliveryTime IS NOT NULL
-- AND s.Open_Time between t_PrevSession.DischargeTime and DateAdd(day,100,t_PrevSession.DischargeTime))
ORDER BY S.MRN,MT.CHARTSTARTTIME