473,395 Members | 1,504 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.

Access ORDER BY / UNION question

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)
Expand|Select|Wrap|Line Numbers
  1. SELECT Aa, Bb, Cc, Dd FROM Table1
  2. WHERE Cc=3
  3. ORDER BY Aa, Bb, Dd
with Query2 (example)
Expand|Select|Wrap|Line Numbers
  1. SELECT Aa, Bb, Cc, Dd FROM Table1
  2. WHERE Cc<>3
  3. ORDER BY Cc, Aa, Bb, Dd
and produce a combined output similar to this (minus header)
Expand|Select|Wrap|Line Numbers
  1. Aa    Bb    Cc    Dd
  2. ---------------------------
  3. Bremmer    Amy    3    455
  4. Bremmer    Bill    3    478
  5. Connor    Carl    3    978
  6. Durf    Dan    3    670
  7. Ensign    Ed    3    406
  8. Barnes    Fred    1    748
  9. Bremmer    Bill    1    885
  10. Finn    Fiona    1    054
  11. Creedy    Cal    2    990
  12. Enfield    Earl    2    031
  13. Anon    Alfred    4    054
  14. Dempsy    Don    4    990
  15. Dempsy    Sue    4    031
  16. Swartz    Sue    5    036
  17.  
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.
Apr 11 '07 #1
6 3562
Should have been more specific about 'no luck';
Q1 by itself works...
Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.Aa AS [ALabl], Table1.Bb AS [BLabl], Table1.Cc AS [CLabl], Table1.Dd AS [DLabl]
  2. 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
  3. WHERE Cc=3
  4. ORDER BY Aa, Bb, Dd;
As does Q2...
Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.Aa AS [ALabl], Table1.Bb AS [BLabl], Table1.Cc AS [CLabl], Table1.Dd AS [DLabl]
  2. 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
  3. WHERE Cc<>3
  4. 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...
Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
  2. FROM (SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
  3. 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
  4. WHERE Cc=3
  5. ORDER BY Aa, Bb, Dd)
  6. UNION ALL
  7. (SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
  8. 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
  9. WHERE Cc<>3
  10. ORDER BY Cc, Aa, Bb, Dd);
UNION without ALL returns records completely randomized...
Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
  2. FROM (SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
  3. 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
  4. WHERE Cc=3
  5. ORDER BY Aa, Bb, Dd)
  6. UNION
  7. (SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
  8. 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
  9. WHERE Cc<>3
  10. ORDER BY Cc, Aa, Bb, Dd);
This gives "syntax error in 'Dd UNION ALL SELECT Table1.Aa"...
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO TmpTbl (Cc, Aa, Bb, Dd)
  2. SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
  3. 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
  4. WHERE Cc=3
  5. ORDER BY Aa, Bb, Dd
  6. UNION ALL
  7. SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
  8. 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
  9. WHERE Cc<>3
  10. ORDER BY Cc, Aa, Bb, Dd;
This gives "syntax error in INSERT INTO statement"...
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO TmpTbl (Cc, Aa, Bb, Dd)
  2. (SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
  3. 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
  4. WHERE Cc=3
  5. ORDER BY Aa, Bb, Dd)
  6. UNION ALL
  7. (SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
  8. 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
  9. WHERE Cc<>3
  10. ORDER BY Cc, Aa, Bb, Dd);
And this returns records with no 1,2 grouping, and corrupts the query with '[%$##@_Alias]' codes...
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO TmpTbl (Aa, Bb, Cc, Dd)
  2. SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
  3. FROM (SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
  4. 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
  5. WHERE Cc=3
  6. ORDER BY Aa, Bb, Dd
  7. UNION ALL
  8. SELECT Table1.Aa, Table1.Bb, Table1.Cc, Table1.Dd
  9. 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
  10. WHERE Cc<>3
  11. ORDER BY Cc, Aa, Bb, Dd);
The newbie says 'thanks for your patience'.
Apr 11 '07 #2
Rabbit
12,516 Expert Mod 8TB
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)
Expand|Select|Wrap|Line Numbers
  1. SELECT Aa, Bb, Cc, Dd FROM Table1
  2. WHERE Cc=3
  3. ORDER BY Aa, Bb, Dd
with Query2 (example)
Expand|Select|Wrap|Line Numbers
  1. SELECT Aa, Bb, Cc, Dd FROM Table1
  2. WHERE Cc<>3
  3. ORDER BY Cc, Aa, Bb, Dd
and produce a combined output similar to this (minus header)
Expand|Select|Wrap|Line Numbers
  1. Aa    Bb    Cc    Dd
  2. ---------------------------
  3. Bremmer    Amy    3    455
  4. Bremmer    Bill    3    478
  5. Connor    Carl    3    978
  6. Durf    Dan    3    670
  7. Ensign    Ed    3    406
  8. Barnes    Fred    1    748
  9. Bremmer    Bill    1    885
  10. Finn    Fiona    1    054
  11. Creedy    Cal    2    990
  12. Enfield    Earl    2    031
  13. Anon    Alfred    4    054
  14. Dempsy    Don    4    990
  15. Dempsy    Sue    4    031
  16. Swartz    Sue    5    036
  17.  
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT Aa, Bb, Cc, Dd
  2. FROM Table1
  3. 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.
Apr 11 '07 #3
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?).
Apr 11 '07 #4
Rabbit
12,516 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT Aa, Bb, Cc, Dd
  2. FROM Table1
  3. ORDER BY IIf([Cc] = 3, 0, [Cc]);
Apr 11 '07 #5
All hail Rabbit! What ended up working was a slight tweak on your solution:
Expand|Select|Wrap|Line Numbers
  1. SELECT Aa, Bb, Cc, Dd
  2. FROM Table1
  3. 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!
Apr 13 '07 #6
Rabbit
12,516 Expert Mod 8TB
Not a problem, good luck.
Apr 13 '07 #7

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

Similar topics

3
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...
4
by: Kent Eilers | last post by:
The following query changes when I save it: ================================================================== SELECT AcctID FROM (SELECT as AcctID FROM tblOrderHeader UNION
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 following SQL code in Access 2000, I get unexpected...
2
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)...
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 FROM ; Append Statement INSERT INTO (, , )...
1
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...
8
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...
11
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...
16
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...
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
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?
1
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...
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
tracyyun
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...

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.