Problems with union query

pseudonym44

New Member
Joined
May 4, 2011
Messages
1
I am using four select queries joined by UNION to populate a recordsheet in Access 2007 as the basis for a report. (To save space, I'll just use two of them here.)

The recordsheet ends up with 6 columns - Group, Metric, Details, FY 2009, FY 2010, and FY 2011. Here's the first query, qryDrugSeizures_FYGrpXtab. It works fine by itself.

TRANSFORM Sum(Format([HQQuantity],"#,##0.000")) AS GrpTotal SELECT tblDrugSeizures.Group, "Drug Seizures" AS Metric, tblDrugSeizures.[Drug] AS Details
FROM tblDrugSeizures
WHERE (((tblDrugSeizures.DateSeized)>#9/30/2008#))
GROUP BY tblDrugSeizures.Group, "Drug Seizures", tblDrugSeizures.[Drug]
ORDER BY tblDrugSeizures.Group, tblDrugSeizures.[Drug]
PIVOT WhatFY([DateSeized]) In ("FY 2009","FY 2010", "FY 2011" );

Here's the second query - qryArrests_FYGrpXtab - it also works fine by itself.

TRANSFORM Count(tblArrests.PageNo) AS CountOfPageNo SELECT tblArrests.Group, "Arrests" AS Metric, "" AS Details
FROM tblArrests
WHERE (((tblArrests.ArrestDate)>#9/30/2008#))
GROUP BY tblArrests.Group, "Arrests", ""
ORDER BY tblArrests.Group
PIVOT WhatFY([ArrestDate]) In ("FY 2009","FY 2010", "FY 2011" );

This Union query also works fine:

Select * from qryDrugSeizures_FYGrpXtab
UNION
Select * from qryArrests_FYGrpXtab;

But when I take the two queries, join them with UNION, remove the first semi-colon and move the ORDER BY clause in the first query to the end, I get a "Syntax error in Transform Statment" error message with the word "UNION" highlighted. Here is the query I'd like to use, the one that gives the error:

TRANSFORM Sum(Format([HQQuantity],"#,##0.000")) AS GrpTotal SELECT tblDrugSeizures.Group, "Drug Seizures" AS Metric, tblDrugSeizures.[Drug] AS Details
FROM tblDrugSeizures
WHERE (((tblDrugSeizures.DateSeized)>#9/30/2008#))
GROUP BY tblDrugSeizures.Group, "Drug Seizures", tblDrugSeizures.[Drug]
PIVOT WhatFY([DateSeized]) In ("FY 2009","FY 2010", "FY 2011" )
UNION
TRANSFORM Count(tblArrests.PageNo) AS CountOfPageNo SELECT tblArrests.Group, "Arrests" AS Metric, "" AS Details
FROM tblArrests
WHERE (((tblArrests.ArrestDate)>#9/30/2008#))
GROUP BY tblArrests.Group, "Arrests", ""
PIVOT WhatFY([ArrestDate]) In ("FY 2009","FY 2010", "FY 2011" )
ORDER BY tblDrugSeizures.Group, Metric, Details;

I would like to use the elaborated SQL so I can use variables for the date in the Where clause and for the three columns for fiscal years. I've tried making changes in the ORDER BY clause, since that's the only one that changes from the separate SQL statements to the UNION statement. Any help would be appreciated. Thanks in advance.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Have you tried using a UNION query to return all the data then using that for the crosstab (TRANSFORM) query?
 
Upvote 0

Forum statistics

Threads
1,215,169
Messages
6,123,412
Members
449,098
Latest member
ArturS75

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top