我正在尝试使此代码正常工作。我需要11条记录,但此查询得到28条记录。通过取消注释约会我可以得到11分,但记录有误。括号中的AND子句似乎无法过滤。谁能给我关于我做得不好的想法?(不是最好的工作):
create table #enc
(birthdate date,pidd numeric(19), id numeric(20),lastn varchar(30),firstn
varchar(30), cdate date)
insert into #enc
select pp.Birthdate,PP.PID, pp.patientprofileid, pp.last, pp.first,
cast(CAST(DateAdd(s, CAST(Left(d.CLINICALDATE,Len(d.CLINICALDATE)-6) AS
DECIMAL(20,0)),'Jan 01 1960') AS DATE) as DATE) as visit
FROM DOCUMENT d
--INNER JOIN Appointments a
--ON d.AppointmentsId = a.AppointmentsId
INNER JOIN PatientVisit v
ON d.PatientVisitId = v.PatientVisitId
inner join patientprofile pp on pp.PId = d.PID
inner join ORDERS o on o.PID= pp.PId
inner join ORDERCODES oc on oc.CODE = o.CODE
where DOCTYPE in(1) and d.status in( 's','u','h','a') and d.USRID
=xxxxx
((((and o.CODE not in('00100','00103','00110','00113','00126'))))
and oc.ORDERTYPE not in('t')
AND CAST(VISIT AS DATE) BETWEEN '10/01/2018' AND '10/31/2018'
select distinct cdate, * from #enc order by lastn
答案 0 :(得分:0)
可能“文档”表和“约会”表具有多对多关系,这意味着一个文档可能链接了多个约会记录。这将导致重复的记录。试试这个:
create table #enc
(birthdate date,pidd numeric(19), id numeric(20),lastn varchar(30),first
nvarchar(30), cdate date)
insert into #enc
select
pp.Birthdate,PP.PID, pp.patientprofileid, pp.last, pp.first,
cast(CAST(DateAdd(s, CAST(Left(d.CLINICALDATE,Len(d.CLINICALDATE)-6) AS DECIMAL(20,0)),'Jan 01 1960') AS DATE) as DATE) as visit
FROM DOCUMENT d
INNER JOIN (
select distinct AppointmentsId from Appointments
) a ON d.AppointmentsId = a.AppointmentsId
INNER JOIN PatientVisit v
ON d.PatientVisitId = v.PatientVisitId
inner join patientprofile pp on pp.PId = d.PID
inner join ORDERS o on o.PID= pp.PId
inner join ORDERCODES oc on oc.CODE = o.CODE
where DOCTYPE in(1) and d.status in( 's','u','h','a') and d.USRID
=xxxxx
((((and o.CODE not in('00100','00103','00110','00113','00126'))))
and oc.ORDERTYPE not in('t')
AND CAST(VISIT AS DATE) BETWEEN '10/01/2018' AND '10/31/2018'
select distinct cdate, * from #enc order by lastn