Query error with aggregate and criteria

pbassett

Active Member
Joined
May 5, 2004
Messages
358
I'm trying in a single query to exclude variances of $0 using data from 2 tables. I create a temp table from 2 queries and query that with aggregates and criteria.
<code>
SELECT x.Vendor, Sum(x.Budget) AS Budget, Sum(x.Actuals) AS Actuals, [Budget]-[Actuals] AS Variance
FROM (
SELECT Vendor, 0 AS Budget, Actuals FROM [Vendor Actuals]
UNION ALL
SELECT Vendor, Budget, 0 AS Actuals FROM [Vendor Budget]
) AS x
GROUP BY x.Vendor
HAVING ((([Budget]-[Actuals])<>0));
</code>
The HAVING statement causes this error:
<code>
You tried to execute a query that does not include the specified expression 'Not [Budget]-[Actuals])=0' as part of an aggregate function."
</code>
Using 2 queries I can get the results, where the first query doesn't have the criteria and the second query calls that query with criteria. Of course I prefer avoiding the need for 2 queries.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I guess you want a where clause not a having clause?

Such as:

Code:
SELECT x.Vendor, Sum(x.Budget) AS Budget, Sum(x.Actuals) AS Actuals, [Budget]-[Actuals] AS Variance
FROM (
SELECT Vendor, 0 AS Budget, Actuals FROM [Vendor Actuals]
UNION ALL
SELECT Vendor, Budget, 0 AS Actuals FROM [Vendor Budget]
) AS x 
WHERE ((([Budget]-[Actuals])<>0))
GROUP BY x.Vendor
;


Given the definition of your subquery X, this is also equivalent:
Code:
SELECT x.Vendor, Sum(x.Budget) AS Budget, Sum(x.Actuals) AS Actuals, [Budget]-[Actuals] AS Variance
FROM (
SELECT Vendor, 0 AS Budget, Actuals FROM [Vendor Actuals] WHERE Actuals <> 0
UNION ALL
SELECT Vendor, Budget, 0 AS Actuals FROM [Vendor Budget] WHERE Budget <> 0
) AS x 
GROUP BY x.Vendor
;
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,150
Members
448,552
Latest member
WORKINGWITHNOLEADER

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