我真的希望你能帮我解决这个问题。这是我当前的代码,到目前为止,它是我能做的最好的代码。
SELECT
max(t2.pmskey) as pmskey, max(cast(t1.recdate as datetime)) as recdate,
max(t2.mrtype) as mrtype, max(cast(t1.mrdate as datetime)) as mrdate,
max(t2.CaseNo) as CaseNo, max(t2.pmclin) as pmclin,
max(cast(t1.nexteval as datetime)) as nexteval,
max(cast(t1.repdate as datetime)) as repdate,
max(t3.mrprocedure) as mrprocedure, max(t2.med_stat) as med_stat,
max(cast(t1.med_stateff as datetime)) as med_stateff,
max(t2.clincontact) as clincontact,
max(cast(t1.datemodf as datetime)) as datemodf, max(t2.modfby) as modfby,
max(cast(t1.inceptiondate as datetime)) as inceptiondate,
max(t2.createdby) as createdby, max(cast(t1.date_ent as datetime)) as date_ent,
max(t2.ppihandler) as ppihandler
FROM
tblpms as t1
JOIN
(
select * from tblpms where lower(CaseNo) like '%tr13-011%'
AND
cast(mrdate as datetime) IN (select max(cast(mrdate as datetime))
from tblpms where lower(CaseNo) like '%tr13-011%')
) as t2
on t1.CaseNo COLLATE DATABASE_DEFAULT = t2.CaseNo COLLATE DATABASE_DEFAULT
JOIN
(
select * from tblpms where lower(CaseNo) like '%tr13-011%'
AND
lower(mrprocedure) is not null and cast(nexteval as datetime)
in (select max(cast(nexteval as datetime)) from
tblpms where lower(CaseNo) like '%tr13-011%')
) as t3
on t1.CaseNo COLLATE DATABASE_DEFAULT = t2.CaseNo COLLATE DATABASE_DEFAULT
and lower(t2.CaseNo) like '%tr13-011%'
的标准lower(CaseNo)
在所有连接中都被复制,我不知道如何减少这些。我相信有些事情是可以做的。如果还有其他可以优化的地方,请将其包含在答案中。
这是我当前基于以下答案的查询..
SELECT
max(t2.pmskey) as pmskey, max(cast(t1.recdate as datetime)) as recdate,
max(t2.mrtype) as mrtype, max(cast(t1.mrdate as datetime)) as mrdate,
max(t2.CaseNo) as CaseNo, max(t2.pmclin) as pmclin,
max(cast(t1.nexteval as datetime)) as nexteval,
max(cast(t1.repdate as datetime)) as repdate,
max(t3.mrprocedure) as mrprocedure, max(t2.med_stat) as med_stat,
max(cast(t1.med_stateff as datetime)) as med_stateff,
max(t2.clincontact) as clincontact,
max(cast(t1.datemodf as datetime)) as datemodf, max(t2.modfby) as modfby,
max(cast(t1.inceptiondate as datetime)) as inceptiondate,
max(t2.createdby) as createdby, max(cast(t1.date_ent as datetime)) as date_ent,
max(t2.ppihandler) as ppihandler
FROM
tblpms as t1
JOIN
(
select * from tblpms where CaseNo = 'TR13-011-CRW'
AND
cast(mrdate as datetime) IN (select max(cast(mrdate as datetime))
from tblpms where CaseNo = 'TR13-011-CRW')
) as t2
on t1.CaseNo COLLATE DATABASE_DEFAULT = t2.CaseNo COLLATE DATABASE_DEFAULT
JOIN
(
select * from tblpms where CaseNo = 'TR13-011-CRW'
AND
lower(mrprocedure) is not null and cast(nexteval as datetime)
in (select max(cast(nexteval as datetime)) from
tblpms where CaseNo = 'TR13-011-CRW')
) as t3
on t1.CaseNo COLLATE DATABASE_DEFAULT = t2.CaseNo COLLATE DATABASE_DEFAULT
and CaseNo = 'TR13-011-CRW'