I really hope you could help me with this one. This is my current code and it is, so far, the best code I could make.
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%'
the criteria for lower(CaseNo)
is replicated in all the joins and I don't know how to lessen those. I believe there is something that could be done. And if there are any other things that could be optimized, please include it in the answer.
this is my current query based on the answers below..
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'