Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Help with query

$
0
0
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 1WHEN 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 1I used the following but it is not working. ROW_NUMBER() OVER (PARTITION BY S.SESSIONID ORDER BY S.MRN) AS VISIT_IDI 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_IDFROM dbo.BLSession_Extended AS S LEFT OUTER JOINdbo.MO_DeliveryTypePatient_table AS DT ON S.sessionID = DT.SessionID INNER JOINdbo.MO_Times MT ON S.sessionId = MT.SessionIDWHERE --(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 ('')--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

Viewing all articles
Browse latest Browse all 3145

Trending Articles