473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Inserting last records into another table

3
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)
Jul 17 '08 #1
4 1472
Delerna
1,134 Expert 1GB
Hi Pete....welcome

Short answer ...... No you don't have to give up on access.

I must say I don't understand how that query worked in SQLSever 2005, I have never used 2005 but it looks very strange to my SQLServer 2000 experience.

As a first attempt I would write it something like this for access
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO LastLabResult
  2. SELECT a.Patient,LabResult1,LabResult2,LabResultn
  3. FROM PatientDetail a
  4. left join
  5. (   Select Patient,LabResult1
  6.     FROM
  7.     (   select Patient,max(ResultDate) as ResDte from View1 group by Patient
  8.     ) a
  9.     join View1 b on a.Patient=b.Patient and a.ResDte=b.ResultDate
  10. )T1 on a.Patient=T1.Patient
  11. left join
  12. (   Select Patient,LabResult2
  13.     FROM
  14.     (   select Patient,max(ResultDate) as ResDte from View2 group by Patient
  15.     ) a
  16.     join View1 b on a.Patient=b.Patient and a.ResDte=b.ResultDate
  17. )T2 on a.Patient=T2.Patient
  18. left join
  19. (   Select Patient,LabResultn
  20.     FROM
  21.     (   select Patient,max(ResultDate) as ResDte from View3 group by Patient
  22.     ) a
  23.     join View1 b on a.Patient=b.Patient and a.ResDte=b.ResultDate
  24. )T3 on a.Patient=T3.Patient
  25.  
I hope I understand what you are doing with your orriginal query. I think I do.
I also Hope the above works for you as I could not test it as I don't have the tables/views or data.

Good luck, you should be able to achieve it one way or another
Jul 17 '08 #2
PeteM
3
I am told from another forum it (my original SQL) works with SQLServer 2005 because 2005 has "With Ties option and ties are implicitly assumed."

It can be made to work in Access by doing a sort on all fields in the view but is very very slow and have been advised to go for joins as you suggested.

However, your code works (Had to make an INNER JOIN where you had JOIN after the LEFT JOIN - hope that makes sense) for one LabTest but wont extend with further a LEFT JOIN - syntax error missing operator?

Pete
Jul 18 '08 #3
PeteM
3
Found the answer to the missing operator error at:

http://nm1m.blogspot.com/2007/10/multiple-left-joins-in-ms-access.html

“I had forgotten about this one. In Microsoft Access, if you want to do more than one LEFT JOIN, you have to use parenthesis in the FROM clause. So, for example, instead of just plain old:

SELECT a.columna, b.columnb, c.columnc
FROM tablea AS a LEFT JOIN tableb AS b ON a.id = b.id LEFT JOIN tablec AS c ON a.id = c.id

you would have to do the following:

SELECT a.columna, b.columnb, c.columnc
FROM ((tablea AS a) LEFT JOIN tableb AS b ON a.id = b.id) LEFT JOIN tablec AS c ON a.id = c.id

Otherwise, you get a "Missing Operator" error.”
Jul 18 '08 #4
Delerna
1,134 Expert 1GB
AHA, that explains why access puts all those brackets everywhere when you use its query wizard. Never worried about it though because at work I always use SQLServer or DB2 for the backend and all queries are as Views,Stored procs and UDF's in SQL Server. At most I use access as the front end to those! Don't even do that much any more because we now use the intranet for our WEB based front end (easier delivery of modifications/fixes/etc to multiple and varied users).

But now, thanks to your post, I know why they are there. Probably explains why some of my queries refuse to work in access :)
Jul 18 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
20
by: Guru | last post by:
Hi I have a table which contains number of rows. I want to fetch the last 5 records from the table. I know for the first 'n' records we can use FETCH FIRST n ROWS ONLY command. But i want to...
2
by: Maks Romih | last post by:
I'm not experienced in db2, but I have to write some rather complex processing with stored procedures for db2 on as400. The database consists of some twenty tables and is prepared in advance so...
7
by: ebindia0041 | last post by:
This is like the bug from hell. It is kind of hard to explain, so please bear with me. Background Info: SQL Server 7.0, Asp.net 1.1 with c# I'm inserting simple records into a table. But one...
13
by: imnewtoaccess | last post by:
Hi, I am getting errors while inserting records in one table from another. These are the structures of two tables : file51tm_new RecordType Text
0
by: toyin | last post by:
hello, pls help look through this code its not inserting the record in dataset into the another database. i want to insert the row in the dataset into another table in another database. pls help....
6
by: ashes | last post by:
Hi, I am creating an ecommerce website using Microsoft Visual Studio, VB.Net and MS Access 2003. I am new to VB.Net When someone wants to register on the website, they fill out a form and the...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
5
by: rando1000 | last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record. ...
8
by: rando1000 | last post by:
I have a VBA program that loops through a table of table names and imports the records from those tables into another table. In some instances (not every time), the inserting gets confused, and I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.