Hello. I have an Access2003 db that I need to create a 'combined' query for. This query will then become the source for a listbox. The query needs to combine Query1 (example) - SELECT Aa, Bb, Cc, Dd FROM Table1
-
WHERE Cc=3
-
ORDER BY Aa, Bb, Dd
with Query2 (example) - SELECT Aa, Bb, Cc, Dd FROM Table1
-
WHERE Cc<>3
-
ORDER BY Cc, Aa, Bb, Dd
and produce a combined output similar to this (minus header) - Aa Bb Cc Dd
-
---------------------------
-
Bremmer Amy 3 455
-
Bremmer Bill 3 478
-
Connor Carl 3 978
-
Durf Dan 3 670
-
Ensign Ed 3 406
-
Barnes Fred 1 748
-
Bremmer Bill 1 885
-
Finn Fiona 1 054
-
Creedy Cal 2 990
-
Enfield Earl 2 031
-
Anon Alfred 4 054
-
Dempsy Don 4 990
-
Dempsy Sue 4 031
-
Swartz Sue 5 036
-
When run separately each query works and sorts successfully using ORDER BY, but when queries are combined with UNION or UNION ALL sorting breaks within each query. The ORDER BY won't work over the whole group because of the 1,2 formatting that must be maintained. I've tried several solutions posted to other sites for ORDER BY/UNION with no luck. Any help would be appreciated. Thanks.
6 3562
Should have been more specific about 'no luck';
Q1 by itself works... - SELECT Table1.Aa AS [ALabl], Table1.Bb AS [BLabl], Table1.Cc AS [CLabl], Table1.Dd AS [DLabl]
-
FROM Table4 RIGHT JOIN (Table3 INNER JOIN (Table2 RIGHT JOIN Table1 ON Table2.Tbl2Index=Table1.Tbl2Curr) ON Table3.Tbl3Index=Table1.Tbl3Curr) ON Table4.Tbl4Index = Table1.Tbl4Curr
-
WHERE Cc=3
-
ORDER BY Aa, Bb, Dd;
As does Q2... - SELECT Table1.Aa AS [ALabl], Table1.Bb AS [BLabl], Table1.Cc AS [CLabl], Table1.Dd AS [DLabl]
-
FROM Table4 RIGHT JOIN (Table3 INNER JOIN (Table2 RIGHT JOIN Table1 ON Table2.Tbl2Index=Table1.Tbl2Curr) ON Table3.Tbl3Index=Table1.Tbl3Curr) ON Table4.Tbl4Index = Table1.Tbl4Curr
-
WHERE Cc<>3
-
ORDER BY Cc, Aa, Bb, Dd;
But this attempt at a UNION returns the records in Q1,Q2 format, but faulty sorting withing Q groups... - SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
-
FROM (SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
-
FROM Table4 RIGHT JOIN (Table3 INNER JOIN (Table2 RIGHT JOIN Table1 ON Table2.Tbl2Index=Table1.Tbl2Curr) ON Table3.Tbl3Index=Table1.Tbl3Curr) ON Table4.Tbl4Index = Table1.Tbl4Curr
-
WHERE Cc=3
-
ORDER BY Aa, Bb, Dd)
-
UNION ALL
-
(SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
-
FROM Table4 RIGHT JOIN (Table3 INNER JOIN (Table2 RIGHT JOIN Table1 ON Table2.Tbl2Index=Table1.Tbl2Curr) ON Table3.Tbl3Index=Table1.Tbl3Curr) ON Table4.Tbl4Index = Table1.Tbl4Curr
-
WHERE Cc<>3
-
ORDER BY Cc, Aa, Bb, Dd);
UNION without ALL returns records completely randomized... - SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
-
FROM (SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
-
FROM Table4 RIGHT JOIN (Table3 INNER JOIN (Table2 RIGHT JOIN Table1 ON Table2.Tbl2Index=Table1.Tbl2Curr) ON Table3.Tbl3Index=Table1.Tbl3Curr) ON Table4.Tbl4Index = Table1.Tbl4Curr
-
WHERE Cc=3
-
ORDER BY Aa, Bb, Dd)
-
UNION
-
(SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
-
FROM Table4 RIGHT JOIN (Table3 INNER JOIN (Table2 RIGHT JOIN Table1 ON Table2.Tbl2Index=Table1.Tbl2Curr) ON Table3.Tbl3Index=Table1.Tbl3Curr) ON Table4.Tbl4Index = Table1.Tbl4Curr
-
WHERE Cc<>3
-
ORDER BY Cc, Aa, Bb, Dd);
This gives "syntax error in 'Dd UNION ALL SELECT Table1.Aa"... - INSERT INTO TmpTbl (Cc, Aa, Bb, Dd)
-
SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
-
FROM Table4 RIGHT JOIN (Table3 INNER JOIN (Table2 RIGHT JOIN Table1 ON Table2.Tbl2Index=Table1.Tbl2Curr) ON Table3.Tbl3Index=Table1.Tbl3Curr) ON Table4.Tbl4Index = Table1.Tbl4Curr
-
WHERE Cc=3
-
ORDER BY Aa, Bb, Dd
-
UNION ALL
-
SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
-
FROM Table4 RIGHT JOIN (Table3 INNER JOIN (Table2 RIGHT JOIN Table1 ON Table2.Tbl2Index=Table1.Tbl2Curr) ON Table3.Tbl3Index=Table1.Tbl3Curr) ON Table4.Tbl4Index = Table1.Tbl4Curr
-
WHERE Cc<>3
-
ORDER BY Cc, Aa, Bb, Dd;
This gives "syntax error in INSERT INTO statement"... - INSERT INTO TmpTbl (Cc, Aa, Bb, Dd)
-
(SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
-
FROM Table4 RIGHT JOIN (Table3 INNER JOIN (Table2 RIGHT JOIN Table1 ON Table2.Tbl2Index=Table1.Tbl2Curr) ON Table3.Tbl3Index=Table1.Tbl3Curr) ON Table4.Tbl4Index = Table1.Tbl4Curr
-
WHERE Cc=3
-
ORDER BY Aa, Bb, Dd)
-
UNION ALL
-
(SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
-
FROM Table4 RIGHT JOIN (Table3 INNER JOIN (Table2 RIGHT JOIN Table1 ON Table2.Tbl2Index=Table1.Tbl2Curr) ON Table3.Tbl3Index=Table1.Tbl3Curr) ON Table4.Tbl4Index = Table1.Tbl4Curr
-
WHERE Cc<>3
-
ORDER BY Cc, Aa, Bb, Dd);
And this returns records with no 1,2 grouping, and corrupts the query with '[%$##@_Alias]' codes... - INSERT INTO TmpTbl (Aa, Bb, Cc, Dd)
-
SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
-
FROM (SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
-
FROM Table4 RIGHT JOIN (Table3 INNER JOIN (Table2 RIGHT JOIN Table1 ON Table2.Tbl2Index=Table1.Tbl2Curr) ON Table3.Tbl3Index=Table1.Tbl3Curr) ON Table4.Tbl4Index = Table1.Tbl4Curr
-
WHERE Cc=3
-
ORDER BY Aa, Bb, Dd
-
UNION ALL
-
SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
-
FROM Table4 RIGHT JOIN (Table3 INNER JOIN (Table2 RIGHT JOIN Table1 ON Table2.Tbl2Index=Table1.Tbl2Curr) ON Table3.Tbl3Index=Table1.Tbl3Curr) ON Table4.Tbl4Index = Table1.Tbl4Curr
-
WHERE Cc<>3
-
ORDER BY Cc, Aa, Bb, Dd);
The newbie says 'thanks for your patience'.
Hello. I have an Access2003 db that I need to create a 'combined' query for. This query will then become the source for a listbox. The query needs to combine Query1 (example) - SELECT Aa, Bb, Cc, Dd FROM Table1
-
WHERE Cc=3
-
ORDER BY Aa, Bb, Dd
with Query2 (example) - SELECT Aa, Bb, Cc, Dd FROM Table1
-
WHERE Cc<>3
-
ORDER BY Cc, Aa, Bb, Dd
and produce a combined output similar to this (minus header) - Aa Bb Cc Dd
-
---------------------------
-
Bremmer Amy 3 455
-
Bremmer Bill 3 478
-
Connor Carl 3 978
-
Durf Dan 3 670
-
Ensign Ed 3 406
-
Barnes Fred 1 748
-
Bremmer Bill 1 885
-
Finn Fiona 1 054
-
Creedy Cal 2 990
-
Enfield Earl 2 031
-
Anon Alfred 4 054
-
Dempsy Don 4 990
-
Dempsy Sue 4 031
-
Swartz Sue 5 036
-
When run separately each query works and sorts successfully using ORDER BY, but when queries are combined with UNION or UNION ALL sorting breaks within each query. The ORDER BY won't work over the whole group because of the 1,2 formatting that must be maintained. I've tried several solutions posted to other sites for ORDER BY/UNION with no luck. Any help would be appreciated. Thanks.
Umm.. you might not realize this but the combination of both these queries is simply: - SELECT Aa, Bb, Cc, Dd
-
FROM Table1
-
ORDER BY Cc, Aa, Bb, Dd
Query 1 returns all rows where Cc = 3, Query 2 returns all rows where Cc <> 3. So the combination of the queries returns all rows.
Umm.. you might not realize this...Query 1 returns all rows where Cc = 3, Query 2 returns all rows where Cc <> 3. So the combination of the queries returns all rows.
Sorry, I should have been clearer: What I need is all rows where Cc = 3 (sorted on Aa, Bb, Dd), followed by all rows where Cc <> 3 (sorted on Cc, Aa, Bb, Dd), in the same singluar set of results, which can be pointed to as a control for a listbox (a query?).
Sorry, I should have been clearer: What I need is all rows where Cc = 3 (sorted on Aa, Bb, Dd), followed by all rows where Cc <> 3 (sorted on Cc, Aa, Bb, Dd), in the same singluar set of results, which can be pointed to as a control for a listbox (a query?).
I see, try this: - SELECT Aa, Bb, Cc, Dd
-
FROM Table1
-
ORDER BY IIf([Cc] = 3, 0, [Cc]);
All hail Rabbit! What ended up working was a slight tweak on your solution: - SELECT Aa, Bb, Cc, Dd
-
FROM Table1
-
ORDER BY IIf([Cc] = 3, 0, [Cc]), Aa, Bb, Dd;
More practice needed in riding the VBA/SQL fence. Many thanks for both the expertise and the patience!
Not a problem, good luck.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Blake Caraway |
last post by:
All,
I've seen several posts regarding using UNION or UNION ALL to mash
together two or more resultsets into a single result set, but can't
seem to find enough info here to help me answer my...
|
by: Kent Eilers |
last post by:
The following query changes when I save it:
==================================================================
SELECT
AcctID
FROM
(SELECT
as AcctID
FROM tblOrderHeader
UNION
|
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 following SQL code in Access 2000, I get unexpected...
|
by: marco |
last post by:
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)...
|
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 FROM ;
Append Statement
INSERT INTO (, , )...
|
by: smaczylo |
last post by:
Hello, I've recently been asked to work with Microsoft Access, and
while I feel quite comfortable with Excel, I'm at a complete loss with
databases. If someone could help me with this issue I'm...
|
by: RJMac |
last post by:
I’m using ADO 2.6 in VB6 to read in data from a Union query (qryJoinBalanceData) in Access 2000. The Union query contains 3 sub-queries, each of which joins several tables, but they all generate...
|
by: Israel |
last post by:
I've trying to write a query that seems like it should be simple but
for some reason my attempts are not working. This is really a general
SQL quesion and doesn't pertain to MySQL but I couldn't...
|
by: anon.asdf |
last post by:
Hi!
On a machine of *given architecture* (in terms of endianness etc.), I
want to access the individual bytes of a long (*once-off*) as fast as
possible.
Is version A, version B, or version...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |