473,548 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access ORDER BY / UNION question

4 New Member
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 3575
halberd4
4 New Member
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 Recognized Expert Moderator MVP
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
halberd4
4 New Member
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 Recognized Expert Moderator MVP
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
halberd4
4 New Member
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 Recognized Expert Moderator MVP
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
11963
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 particular question. I have a stored procedure that gets the column names in a particular format (i.e....
4
1678
by: Kent Eilers | last post by:
The following query changes when I save it: ================================================================== SELECT AcctID FROM (SELECT as AcctID FROM tblOrderHeader UNION
3
1925
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 results: SELECT MY_FIELD FROM ( SELECT NULL AS MY_FIELD FROM DUAL
2
9746
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) problem can avoid these technicalities: the original table has columns A1, A2, B1, B2, C1, C2.
4
52408
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 (, , ) VALUES ('value1', #value2#, value3); This assumes value1 is a string, value2 is a date and value 3 is some other datatype
1
9757
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 having I'd be most appreciative. The database is already constructed, I'm just wanting to export the data to an excel file. In short, I'm hoping...
8
2663
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 exactly the same fields containing the same type of data. I’m using qryJoinBalanceData as the source for a Group By and Sum query which compresses 4600...
11
9670
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 find a generic database discussion group except on on advancement and theory and this is really a basic query construction question. Just say I...
16
2195
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 C better? Are there other alternatives? /**** Version A ******/
0
7711
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7954
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5085
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1932
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.