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 3575
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'.
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) - 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?).
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: - 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!
Rabbit 12,516
Recognized Expert Moderator MVP
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 particular
question.
I have a stored procedure that gets the column names in a particular
format (i.e....
|
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
results:
SELECT MY_FIELD
FROM
(
SELECT NULL AS MY_FIELD FROM DUAL
|
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.
|
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
| |
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...
|
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...
|
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...
|
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 ******/
|
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. ...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |