How to improve this query?
select ID, Name
,(select top 1 Item FROM Items where BeneficiaryID = a.ID
order by PickupDateTime desc) as Item
,(select top 1 FontColor FROM Items where BeneficiaryID = a.ID
order by PickupDateTime desc) as FontColor
,(select top 1 BackColor FROM Items where BeneficiaryID = a.ID
order by PickupDateTime desc) as BackColor
FROM Beneficiary a
where Name like N'%Sam%'
When I try the 3 fields in the same subquery I get:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
I need to get the beneficiary data with the latest items that was picked up stored in the items table.
I tried doing it with a left join but did not get the desired results.