Using SQL Server 2005 I have on a number of occasions, succesfully used the following code to create tables with the last dated results or observations for each Patient. However I am now being asked to do the same in Access 2003 and all the variations I can think of return the error message “At most one record can be returned by this subquery”. Do I have to give up with Access?
SELECT Patient,
(SELECT TOP 1 t1.LabResult1
FROM View1 AS t1
WHERE t1.Patient = b.Patient
ORDER BY t1.ResultDate DESC) AS LabResult1,
(SELECT TOP 1 t1.LabResult2
FROM View2 AS t1
WHERE t1. Patient = b.Patient
ORDER BY t1.ResultDate DESC) AS LabResultn,
(SELECT TOP 1 t1.LabResultn
FROM Viewn AS t1
WHERE t1. Patient = b.Patient
ORDER BY t1.ResultDate DESC) AS LabResultn
INTO LastLabResult
FROM (SELECT Patient
FROM PatientDetail) b
View code
create view View1
as
select Patient, ResultDate, LabResult1
from LabResultsTable
where LabResult1 is not null
group by Patient, ResultDate, LabResult1 -- to remove duplicates
(not all tests carried out at the same time so each result has a separate view)
SELECT Patient,
(SELECT TOP 1 t1.LabResult1
FROM View1 AS t1
WHERE t1.Patient = b.Patient
ORDER BY t1.ResultDate DESC) AS LabResult1,
(SELECT TOP 1 t1.LabResult2
FROM View2 AS t1
WHERE t1. Patient = b.Patient
ORDER BY t1.ResultDate DESC) AS LabResultn,
(SELECT TOP 1 t1.LabResultn
FROM Viewn AS t1
WHERE t1. Patient = b.Patient
ORDER BY t1.ResultDate DESC) AS LabResultn
INTO LastLabResult
FROM (SELECT Patient
FROM PatientDetail) b
View code
create view View1
as
select Patient, ResultDate, LabResult1
from LabResultsTable
where LabResult1 is not null
group by Patient, ResultDate, LabResult1 -- to remove duplicates
(not all tests carried out at the same time so each result has a separate view)
Comment