Union Query Not Working Properly

  • Thread starter Thread starter JohnAFT
  • Start date Start date
J

JohnAFT

I am trying to use a Union Query to pull the top 2 records for each group from
the same table. It works for the very first part (it pulls the 2 highest
numbers) but it fails in the other part of the statement...the part that fails
does give 2 records, but they are not the highest numbers.

Table name: growth
Fields: [mem]...field for name of members, (text)
[por].....field for amount (integer)
[dato]....field for date

Below if the SQL statement...why is the first part working and the other part
not?

SELECT TOP 2 growth.mem, growth.por, growth.dato
FROM growth
WHERE (((growth.por)="dw"))
ORDER BY growth.mem DESC
Union
SELECT TOP 2 growth.mem, growth.por, growth.dato
FROM growth
WHERE (((growth.por)="jj"))
ORDER BY growth.mem DESC;
 
The second ORDER BY Clause applies to all the rows that
have been selected by the Union. It doesn't apply to the
second SELECT statement, i.e. the second part of the Union.

Try (NOT tested):

SELECT TOP 2 growth.mem, growth.por, growth.dato
FROM growth
WHERE (((growth.por)="dw"))
ORDER BY growth.mem DESC
Union
SELECT VT.mem, VT.por, VT.dato
FROM
(
SELECT TOP 2 growth.mem, growth.por, growth.dato
FROM growth
WHERE (((growth.por)="jj"))
ORDER BY growth.mem DESC
) As VT

If you use A97, you may need a different syntax for the
SubQuery

HTH
Van T. Dinh
MVP (Access)



-----Original Message-----
I am trying to use a Union Query to pull the top 2 records for each group from
the same table. It works for the very first part (it pulls the 2 highest
numbers) but it fails in the other part of the
statement...the part that fails
does give 2 records, but they are not the highest numbers.

Table name: growth
Fields: [mem]...field for name of members, (text)
[por].....field for amount (integer)
[dato]....field for date

Below if the SQL statement...why is the first part working and the other part
not?

SELECT TOP 2 growth.mem, growth.por, growth.dato
FROM growth
WHERE (((growth.por)="dw"))
ORDER BY growth.mem DESC
Union
SELECT TOP 2 growth.mem, growth.por, growth.dato
FROM growth
WHERE (((growth.por)="jj"))
ORDER BY growth.mem DESC;

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Union Query 0
Union query 5
Secondary Sort for Union Query? 2
Union Query of Two Queries (Part 2) 2
union query 19
Union Query and Field Alias 7
SUM in a UNION query 2
SELECT/UNION query not working 13

Back
Top