Sort in UNION query

G

Guest

Thanks for taking the time to read my question.

I want to sort the data returned so that "All" and "Blank" show up at the
top. I can't get it to do that.

tblCostMaster.CFPROD returns the values, and the Type field will have
nothing in it (Col 1) and has the values of CFPROD in column 2. The first and
second SELECT statements return "Blank" and "All" in the first column, and 0,
and -1 in the second column.

For some reason I can't sort on the first column Type, and get the "Blank"
and "All" values to appear at the top of the returned data. They sort, but
stay at the bottom of the data.

Not sure why.

Thanks for your help,

Brad

Code
===============
SELECT "Blank" as Type, 0 as Junk
FROM tblCostMaster
ORDER BY "Blank" DESC

UNION SELECT "All" as Type, -1 as Junk
FROM tblCostMaster

UNION SELECT "" AS Type, tblCostMaster.CFPROD
FROM tblCostMaster
GROUP BY "", tblCostMaster.CFPROD
HAVING ((Not (tblCostMaster.CFPROD) Is Null));
=================
 
J

John Spencer

In a UNION query the ORDER BY statement uses the field names from the first
Query clause, but it goes at the end of the last query clause

===============
SELECT "Blank" as [Type], 0 as Junk
FROM tblCostMaster

UNION SELECT "All" as [Type], -1 as Junk
FROM tblCostMaster

UNION SELECT "" AS [Type], tblCostMaster.CFPROD
FROM tblCostMaster
GROUP BY "", tblCostMaster.CFPROD
HAVING ((Not (tblCostMaster.CFPROD) Is Null))
ORDER BY [Type] DESC
=================

Be careful, Type is a reserved word in Access. That is why I chose to
surround it with brackets [].
 
G

Guest

Thanks for the help! And for the heads up on TYPE.

I'll change that.

Have a great day,

Brad

John Spencer said:
In a UNION query the ORDER BY statement uses the field names from the first
Query clause, but it goes at the end of the last query clause

===============
SELECT "Blank" as [Type], 0 as Junk
FROM tblCostMaster

UNION SELECT "All" as [Type], -1 as Junk
FROM tblCostMaster

UNION SELECT "" AS [Type], tblCostMaster.CFPROD
FROM tblCostMaster
GROUP BY "", tblCostMaster.CFPROD
HAVING ((Not (tblCostMaster.CFPROD) Is Null))
ORDER BY [Type] DESC
=================

Be careful, Type is a reserved word in Access. That is why I chose to
surround it with brackets [].

Brad said:
Thanks for taking the time to read my question.

I want to sort the data returned so that "All" and "Blank" show up at the
top. I can't get it to do that.

tblCostMaster.CFPROD returns the values, and the Type field will have
nothing in it (Col 1) and has the values of CFPROD in column 2. The first
and
second SELECT statements return "Blank" and "All" in the first column, and
0,
and -1 in the second column.

For some reason I can't sort on the first column Type, and get the "Blank"
and "All" values to appear at the top of the returned data. They sort,
but
stay at the bottom of the data.

Not sure why.

Thanks for your help,

Brad

Code
===============
SELECT "Blank" as Type, 0 as Junk
FROM tblCostMaster
ORDER BY "Blank" DESC

UNION SELECT "All" as Type, -1 as Junk
FROM tblCostMaster

UNION SELECT "" AS Type, tblCostMaster.CFPROD
FROM tblCostMaster
GROUP BY "", tblCostMaster.CFPROD
HAVING ((Not (tblCostMaster.CFPROD) Is Null));
=================
 

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
How do I sort this union query? 2
Union Query 1
Secondary Sort for Union Query? 2
Creating a parameter in a Union Query 2
Union sort 8
Rename fields in Union query? 5
Union Query 2

Top