473,414 Members | 1,738 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,414 software developers and data experts.

Complex SQL statement in MS Access

Zwoker
66
Hi all,

I'm not sure whether this should be posted in the MS Access forum or one of the SQL forums (which one?), so I'll start here.

I'm self taught in the syntax of SQL based queries that I have been writing in VBA in my MS Access 2003 environment, which has proved to be an issue now that I want to do something a bit more complicated than the ordinary, but have no idea how. Googling for an example has failed to find anything close to what I want.

What I'm trying to do is write a single SQL statement to read all the records that match user-selected criteria in a master table, and as part of that, read just one record from a JOIN'ed transaction table, with the record I want being the most recent record for the joined keys. Fortunately there is a date field in the transaction table.

Now I believe that I need some sort of subquery with a TOP 1 in it, with an ORDER BY the transaction date (descending), but trying to integrate the subquery with the JOIN statement has been a complete disaster.

I separated the two activities, doing the initial SELECT over the master and getting all the records that matched the user criteria, and then looping through each of those records to get the most recent transaction record, but the run time for the process went from 35 seconds without the looped read of the transaction table, to over 30 minutes with it present.

Does anyone have a simple example of a JOIN and a subquery that is something like what I'm trying to do, that I can use as the foundation for my query?


Thanks.
Mar 20 '08 #1
4 2224
janders468
112 Expert 100+
I think that the easiest way to do this would be to split it into two queries, the first query should consist of all the records to retrieve from the master table based on whatever criteria. Join this query to the transaction table ordering by the date field descending, which will put the most recent date first and use the top keyword after the select statement just like you suggested.
So it would be

Select TOP 1 Field1, Field2...Fieldn
From TransactionTable Inner Join TransactionTable.KeyField = MasterTableQuery.KeyField
Order By Transaction.DateField Desc

Hope that helps
Mar 20 '08 #2
Zwoker
66
Ok, I hadn't thought of doing it that way. I had it in my head that to make it run quickly I needed to do it as a single query.

Before I read the reply I had already compromised by doing it as a single query with the Joins, and the Where statements from the user selection criteria. I ordered the result by the join keys and the date field. Although this returned a very large number of records, I coded a simple "on change" of the keys in VBA and just kept the first record for each change. A non-clean solution, but one that still runs reasonably quickly.

I'll try doing the second query using the table from the first, as suggested, and see how it goes.

The one problem I am having is that using a connection string approach to my data, rather that just declaring external tables in MS Access, seems to stop me returning the result directly as a table - I have to keep it as a result set. This is because the SELECT INTO statement doesn't seem to like the fact that I'm reading from an external table with the connection string, but trying to write to a local table. Or am I missing something obvious?
Mar 24 '08 #3
janders468
112 Expert 100+
That doesn't seem like it should be a problem. What error does it give you when you try to make it into a table?
Mar 25 '08 #4
Zwoker
66
That doesn't seem like it should be a problem. What error does it give you when you try to make it into a table?
It gives me an Oracle error about the table not existing. Which makes sense, as I don't want (and am not allowed) to create the table there, but want to create it in my local MS Access database where the code is running from.

It might be as simple as the method I'm using to call the SQL that I have built for the read. It looks like this:

Expand|Select|Wrap|Line Numbers
  1. Set InitialRst = New ADODB.Recordset
  2. InitialRst.Open SQLstring, connForDB, adOpenStatic, adLockReadOnly
If the SQLstring contains a SELECT INTO there is no way for it to know that I want the table to be created in the local database (is there?) rather that in the remote Oracle database that the connection string points to.

Alternatively, since I don't know SQL very well, and the syntax for the Oracle version even less, it could be a simple issue with the SQL command I am building. Just how do I write the SQL to allow me to read from the external table, but write to a local (MS Access) one?
If you can give me a simple example it might solve the issue.

I can see I need to find some sort of online tutorial on this subject, which can show me the different options avaiable in using the SQL string I have built over the remote database.
Mar 26 '08 #5

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

Similar topics

39
by: | last post by:
I am trying to run the following agregate function in a parameterized query on Access2000: Min(.*sqr(./.)/) The query saved OK, but an attempt to run it results in the message: The expression...
3
by: Marcus | last post by:
Hi I have a very complex sql query and a explain plan. I found there is a full table scan in ID=9 9 8 TABLE ACCESS (FULL) OF 'F_LOTTXNHIST' (Cost=84573 Card=185892...
116
by: Mike MacSween | last post by:
S**t for brains strikes again! Why did I do that? When I met the clients and at some point they vaguely asked whether eventually would it be possible to have some people who could read the data...
3
by: Susie Swint | last post by:
I have the following IIf statement which worked in Access 95 but will not work in Access 2002. The error message I get is that the expression is typed incorrectly, or is too complex to be...
2
by: Ben de Vette | last post by:
Hi, I'm using the querybuilder when updating a record in a table (Access). However, I get a "Query is too complex" message. The Primary key is autonumbered. Why is it making such a complex...
6
by: Jon Bilbao | last post by:
I´m trying a select clause in two steps because it´s too complex. First: SELECT Reference, Results.idEnsayo, Results.Num_taladro, min(Results.dTime) + 500 AS tIni, max(Results.dTime) - 500 AS...
1
by: Randy Volkart | last post by:
I'm trying to fix a glitch in a complex access database, and have a fairly complex problem... unless there's some obscure easy fix I don't know being fairly new with Access. Basically, the area...
19
by: kawaks40 | last post by:
Hi everyone :) I just recently started using access/sql. and right away I ran into this problem "SQL expression too complex" I google'd a lot on what it means, and the only workaround I've...
3
by: Eric Davidson | last post by:
DB2 9.5 I keep geting the message. SQL0101N The statement is too long or too complex. SQLSTATE=54001 When one of my sql statements takes over 60 seconds to compile the sql statement. Is...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.