以下是我的数据:
MedicalRecordNumber edcdate wk lastappt facility provider
---------------------- ---------- ----- ---------------------- --------------- ------------------------
255599 10/9/2014 37 3/8/2014 0:00 Women's Health Prenatal Registration
255599 10/9/2014 37 6/12/2014 0:00 Women's Health Tarcia
255599 10/9/2014 37 9/4/2014 0:00 Women's Health Beaven
这是我使用的SQL:
Select a.ownerid, p.patientprofileid, p.searchname,
max(a.EmrApptStart) as LastAppt, f.listname as facility, d.ListName as Provider
from Appointments a
left join patientprofile p on p.PatientProfileId= a.OwnerId
inner join DoctorFacility f on f.DoctorFacilityId = a.FacilityId
inner join DoctorFacility d on d.DoctorFacilityId = a.ResourceId
where a.FacilityId in ('127','64') and p.MedicalRecordNumber = '00255599' and a.Status in ('Completed','Arrived')
group by a.OwnerId,p.patientprofileid, p.searchname,f.listname, d.listname
我正在尝试获取最近的日期和他们看到的提供商,当我不在群组中包含提供商时,我得到了我需要的结果,请参阅下文:
Select a.ownerid, p.patientprofileid as MRN, p.searchname,
max(a.EmrApptStart) as LastAppt, f.listname as facility
from Appointments a
left join patientprofile p on p.PatientProfileId= a.OwnerId
inner join DoctorFacility f on f.DoctorFacilityId = a.FacilityId
inner join DoctorFacility d on d.DoctorFacilityId = a.ResourceId
where a.FacilityId in ('127','64') and p.MedicalRecordNumber = '00255599' and a.Status in ('Completed','Arrived')
group by a.OwnerId,p.patientprofileid, p.searchname,f.listname
MRN LastAppt facility
---------- -------------- --------------------
255599 9/4/2014 0:00 Women's Health
如果我在我的select语句中包含Provider,如果我不在Group By子句中包含它,我会收到错误。有什么想法吗?
答案 0 :(得分:0)
好像您对提供商的前1条记录感兴趣。在这种情况下,您可以将提供商添加到group by
子句select top 1 ....
并使用Order by a.EmrApptStart desc
答案 1 :(得分:0)
SELECT
a.PatientProfileId,
a.MedicalRecordNumber,
a.searchname,
a.edcdate,
a.wk ,
b.lastappt, c.NextAppt, c.NextProvider
FROM
(SELECT
PatientProfile.PID,
PatientProfile.PatientProfileID,
PatientProfile.MedicalRecordNumber,
PatientProfile.searchname,
EDCDATE=case when isdate(max(RPTOBS.obsvalue))=1 then max(RPTOBS.obsvalue) end,
WK=case when isdate(max(RPTOBS.obsvalue))=1 then 40-DATEDIFF(d,getdate(),max(RPTOBS.obsvalue))/7 end
FROM OBSHEAD OBSHEAD
INNER JOIN ((PatientProfile PatientProfile
INNER JOIN DOCUMENT DOCUMENT ON PatientProfile.PId=DOCUMENT.PID)
INNER JOIN RPTOBS RPTOBS ON (DOCUMENT.PID=RPTOBS.pid)
AND (DOCUMENT.SDID=RPTOBS.sdid)) ON OBSHEAD.HDID=RPTOBS.hdid
WHERE RPTOBs.hdid='8086' --and PatientProfile.PatientProfileID='255795'
GROUP BY PatientProfile.PID,PatientProfile.PatientProfileID, PatientProfile.MedicalRecordNumber, PatientProfile.searchname)a
INNER JOIN
(SELECT DOCUMENT.PID, p.patientprofileid, LASTAPPT=DATEADD(day,max((DOCUMENT.CLINICALDATE)/1000000/3600/24),'1960-01-01')
FROM DOCUMENT Inner join patientprofile p on document.pid=p.pid
WHERE document.summary like '%Prenatal%' and DOCUMENT.DOCTYPE = '1' --and p.patientProfileID='255795'
GROUP BY DOCUMENT.PID, p.patientprofileid) b
ON a.pid = b.pid
INNER JOIN
(Select --TOP 1
a.ownerid,min(a.ApptStart) as NextAppt, d.listname as NextProvider
FROM Appointments a
INNER JOIN DoctorFacility d on d.DoctorFacilityId = a.ResourceId
WHERE a.FacilityId in ('127','64') and a.Status = 'Scheduled' and a.ApptTypeId in ('15','10','181','25','24','263','235','89','90')
--and a.ownerid = '255795'
GROUP BY a.OwnerId, d.listname, a.ApptStart
-- ORDER BY a.ApptStart ASC
) c
ON a.PatientProfileID=c.ownerid and b.patientprofileid = c.ownerid
WHERE EDCDATE>=cast(GETDATE() as DATE)
GROUP BY
a.PatientProfileId,
a.MedicalRecordNumber,
a.searchname,a.edcdate,
a.wk, b.lastappt, c.NextAppt, c.NextProvider