472,353 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

MS Access 97 / UNION / SELECT INTO

Dear List,

as it seems, MS SQL as used in Access does not allow a select INTO
within a UNION query. Also, it seems that a UNION query can not be used
as a subquery.

Maybe my (simplified) problem can avoid these technicalities: the
original table has columns

A1, A2, B1, B2, C1, C2.

Now, the sum over entries in A1 is taken if the corresponding entries
in A2 satisfy some condition. The same is done for the B- and C-
columns. All those sums should be one single sum, as if the table would
be

F1, F2,

where F2 contains each and all entry/ies from A1, B1, and C1, and F2
those from A2, B2, and C2, and the sum is just taken over those values.

My current solution is like the following UNION-query, with results
saved (by hand) into tbl2.

(SELECT A1 FROM tbl WHERE A2="take me")
UNION ALL
(SELECT B1 FROM tbl WHERE B2="take me")
UNION ALL
(SELECT C1 FROM tbl WHERE C2="take me");

Then, the actual sums are taken by
SELECT SUM(F1) FROM tbl2;

Question 1:
Can the first query use a "SELECT INTO" clause, and if so, where?

Question 2a:
Can the second query use the first one - without the hand-saved
intermediate result?

Question 2b:
If the second query references several fields from the first query,
does Access recompute that first query each time? (A test-query with
ten references has needed about ten times as long.)

Question 3:
Can the two queries be combined into one? Which way is most efficient?
Thanks very much in advance for any advice you can give!

Marco

PS
The data table is quite large, so time IS an issue here. The UNION
query takes about 1.5hrs, and a query referencing the UNION-query about
ten times takes about 15 hours.

Nov 25 '05 #1
2 9570
I think these are about what you are asking. One is for JEt 4.0 the other
for JEt 3.5.Perhaps, if you study them some of your questions will be
answered.

Sub temp40()
Dim sql As String
Dim rst As DAO.Recordset
sql = "SELECT A1 AS F1 FROM tbl WHERE A2 = 'Take Me'"
sql = sql & vbNewLine & "UNION ALL"
sql = sql & vbNewLine & "SELECT B1 AS F1 FROM tbl WHERE B2 = 'Take
Me'"
sql = "SELECT SUM(Subquery.F1) AS SumF1 FROM" & vbNewLine & "(" & sql
& " ) AS Subquery"
Set rst = DBEngine(0)(0).OpenRecordset(sql)
Debug.Print "-----------"
Debug.Print "4.0"
Debug.Print sql
If Not rst.BOF Then
Debug.Print rst.Fields("SumF1").Value
Else
Debug.Print "No records"
End If
Debug.Print "-----------"
Set rst = Nothing
End Sub

Sub temp35()
Dim sql As String
Dim rst As DAO.Recordset
sql = "SELECT A1 AS F1 FROM tbl WHERE A2 = 'Take Me'"
sql = sql & vbNewLine & "UNION ALL"
sql = sql & vbNewLine & "SELECT B1 AS F1 FROM tbl WHERE B2 = 'Take
Me'"
sql = "SELECT SUM(Subquery.F1) AS SumF1 FROM" & vbNewLine & "[" & sql
& " ]. AS Subquery"
Set rst = DBEngine(0)(0).OpenRecordset(sql)
Debug.Print "-----------"
Debug.Print "3.5"
Debug.Print sql
If Not rst.BOF Then
Debug.Print rst.Fields("SumF1").Value
Else
Debug.Print "No records"
End If
Debug.Print "-----------"
Set rst = Nothing
End Sub

--
Lyle Fairfield
Nov 25 '05 #2
Dear Lyle,

thanks a ton for your detailed message. It took me a few days until I
could respond, sorry.

I used the query itself, not VBA, but it works perfectly. Looks like
the []. syntax was the catch. In fact, this query avoids the ten-fold
call to the subquery, and is just as fast as the two queries by hand,
with the advantage of doing everything automatically.

Very awesome, and thanks a bunch again!

Marco Loskamp

Nov 28 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: s_gregory | last post by:
The mdb is considerable size 70 +- mb. A complex union query was working well, but when an additional union select... was added into the query,...
4
by: Kent Eilers | last post by:
The following query changes when I save it: ================================================================== SELECT AcctID FROM (SELECT as...
1
by: matthew kramer | last post by:
Hi, I'm using the reverse pivot technique on an excel spreadsheet to create a list dataset from which I can make a pivot table in excell. The...
3
by: Dan | last post by:
I hate it when people think that their own misunderstandings are bugs in the program, but this time I think I've got something. If I run the...
6
by: das | last post by:
Hello all, I have a table with thousands of rows and is in this format: id col1 col2 col3 col4 --- ------ ----- ------ ...
10
by: Robert | last post by:
I am an attorney in a non-profit organization and a self-taught programmer. I'm trying to create a client db that will allow me to search for...
4
MMcCarthy
by: MMcCarthy | last post by:
To view Access queries in SQL rather than Access query design - open the query design window and change the view to SQL: Select Statement SELECT ...
2
by: jafastinger | last post by:
I have a large union. If I break it into its individual parts they all run quick. The longest is the last select it takes 2 minutes to fetch all...
27
by: MLH | last post by:
How can I turn the following into a make-table query? SELECT & " " & AS Recipient FROM tblVehicleJobs INNER JOIN tblAddnlOwnrs ON...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.