Hi @Hellothere8028 ,
Sorry that I made a small mistake on the join part 'a.ctext=a.ctext ' and I just realized that!
Please refer below query:
select a.ctext,a.vbillid,a.patientid,a.assigndate,IIF(compdate='1900-01-01',NULL,compdate) compdate
,case when a.assigndate> max then b.created else c.created end created
,case when a.assigndate> max then b.blockyn else c.oldvalue end blockyn
from ##vreport a
left join ##client b on a.ctext=b.c_textid and a.patientid=b.id
left join (select ROW_NUMBER() over (partition by c_textid,sourceid order by (select null)) rn, * from ##caud where fieldname ='blockyn') c on a.ctext=c.c_textid and c.sourceid=a.patientid
left join (select c_textid,sourceid,max(created) max from ##caud group by c_textid,sourceid) d on d.c_textid=c.c_textid and d.sourceid=c.sourceid
where c.rn=1
Output:
ctext vbillid patientid assigndate compdate created blockyn
1231 231 12 2020-04-03 2020-04-07 2020-04-01 Y
2311 342 19 2020-02-21 NULL 2020-02-24 N
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.