Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting an error when trying to run this union query

Status
Not open for further replies.

xcmuchip

Programmer
May 31, 2001
70
select date, [N/S], productcode, sum([#CS]) as SumCases from espnorth

UNION select date, [N/S], productcode, sum([#cs]) as SumCases from espsouth
group by date, [n/s], productcode
ORDER BY date, productcode;

Error is "You tried to execute a query that doesn't include the specified expression 'date' as part of an aggregate function." These are pre-existing databases, so the names of the fields are not my idea. If I take the sum function out, the error disappears.

Why am I getting this error? Thanks for your help.
 

Add brackets around the name of the date column.

UNION
SELECT
[date], [N/S],
productcode, sum([#cs]) as SumCases
FROM espsouth
GROUP BY [date], [n/s], productcode
ORDER BY [date], productcode;
Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
 
I've tried that already, thanks. Still get the same error.
 

Either include the Group By in each select statement

select
date, [N/S], productcode,
sum([#CS]) as SumCases
from espnorth
group by date, [n/s], productcode

UNION

select
date, [N/S], productcode,
sum([#CS]) as SumCases
from espsouth
group by date, [n/s], productcode

ORDER BY date, productcode;

Or use the Union query as a sub-query as follows.

Select
[Date], [N/S], productcode,
sum([#CS]) as SumCases
From
(Select date, [N/S], productcode, [#CS]
From espnorth
UNION ALL
Select date, [N/S], productcode, [#CS]
From espsouth) As qry

Group By date, [n/s], productcode
ORDER BY date, productcode;

I recommend using the ALL predicate to prevent Access from sorting the results of the UNION query eliminating duplicates. Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top