The query it is based on runs perfectly fine, but I will post it at
the bottom. Something else weird? It started with a change request.
With my DEV version down, I made a copy of production and added my new
query. When I put too many fields, (about a dozen,) basedon that same
underlying query, it works fine. As I REDUCE the number of fields,
the query somehow becomes too complex.
So here are three queries. The underlying query, which works fine, is
a massive six piece union query:
------------------------------------------------------------
SELECT invoice_d.invoice_id, invoice_d.line_id, term.name,
customer_type.name AS CustomerType, customer.name, invoice_d.item_id,
invoice_d.order_qty, item.short_desc, division.name AS Division, "" AS
Dept, category.name AS Category, brand.name AS Brand, 9140 AS Acct,
invoice_d!cost*invoice_d!ship_qty AS ext_Cost, invoice_h.invoice_date
FROM category INNER JOIN (brand INNER JOIN (division INNER JOIN (term
INNER JOIN (item INNER JOIN (customer_type INNER JOIN (customer INNER
JOIN (invoice_d INNER JOIN invoice_h ON
invoice_d.invoice_id=invoice_h.invoice_id) ON
customer.customer_id=invoice_h.customer_id) ON
customer_type.customer_type_id=customer.customer_type_id) ON
item.item_id=invoice_d.item_id) ON term.term_id=invoice_h.term_id) ON
division.division_id=item.division_id) ON
brand.brand_id=item.brand_id) ON
category.category_id=item.master_category_id
WHERE (((term.name)="Warranty") And
((invoice_h.invoice_date)>=Forms!Sales_Reports_Form!STARTDATE And
(invoice_h.invoice_date)<=Forms!Sales_Reports_Form!ENDDATE) And
((invoice_d.price)=0));
union all
SELECT invoice_d.invoice_id, invoice_d.line_id, term.name,
customer_type.name AS CustomerType, customer.name, invoice_d.item_id,
invoice_d.order_qty, item.short_desc, [Promo recode map].Division AS
Expr1, [Promo recode map].Department AS Expr2, "" AS Category, [Promo
recode map].Brand AS Expr3, [Promo recode map].[Promo Acct] AS Expr4,
invoice_d!cost*invoice_d!ship_qty AS ext_Cost, invoice_h.invoice_date
FROM [Promo recode map] INNER JOIN (term INNER JOIN (item INNER JOIN
(customer_type INNER JOIN (customer INNER JOIN (invoice_d INNER JOIN
invoice_h ON invoice_d.invoice_id=invoice_h.invoice_id) ON
customer.customer_id=invoice_h.customer_id) ON
customer_type.customer_type_id=customer.customer_type_id) ON
item.item_id=invoice_d.item_id) ON term.term_id=invoice_h.term_id) ON
[Promo recode map].[Customer id]=customer.customer_id
WHERE (((term.name)<>"Warranty") And ((customer_type.name)="SAMPLES"
Or (customer_type.name)="PROMO") And
((invoice_h.invoice_date)>=Forms!Sales_Reports_Form!STARTDATE And
(invoice_h.invoice_date)<=Forms!Sales_Reports_Form!ENDDATE) And
((invoice_d.price)=0));
union all
SELECT invoice_d.invoice_id, invoice_d.line_id, term.name,
customer_type.name AS CustomerType, customer.name, invoice_d.item_id,
invoice_d.order_qty, item.short_desc, [Promo recode map].Division AS
Expr1, [Promo recode map].Department AS Expr2, "" AS Category, [Promo
recode map].Brand AS Expr3, [Promo recode map].[Promo Acct] AS Expr4,
invoice_d!cost*invoice_d!ship_qty AS ext_Cost, invoice_h.invoice_date
FROM [Promo recode map] INNER JOIN (term INNER JOIN (item INNER JOIN
(customer_type INNER JOIN (customer INNER JOIN (invoice_d INNER JOIN
invoice_h ON invoice_d.invoice_id=invoice_h.invoice_id) ON
customer.customer_id=invoice_h.customer_id) ON
customer_type.customer_type_id=customer.customer_type_id) ON
item.item_id=invoice_d.item_id) ON term.term_id=invoice_h.term_id) ON
[Promo recode map].[Customer type]=customer_type.name
WHERE (((term.name)<>"Warranty") And ((customer_type.name)="CORE
WHOLESALE" Or (customer_type.name)="EMPLOYEE PURCHASES" Or
(customer_type.name)="FOREIGN DISTRIBUTOR" Or
(customer_type.name)="KEY DISTRIBUTOR" Or (customer_type.name)="KEY
WHOLESALE" Or (customer_type.name)="HOUSE ACCOUNTS" Or
(customer_type.name)="FOREIGN WHOLESALE") And
((invoice_h.invoice_date)>=Forms!Sales_Reports_Form!STARTDATE And
(invoice_h.invoice_date)<=Forms!Sales_Reports_Form!ENDDATE) And
((invoice_d.price)=0));
union all
SELECT invoice_d.invoice_id, invoice_d.line_id, term.name,
customer_type.name AS CustomerType, customer.name, invoice_d.item_id,
invoice_d.order_qty, item.short_desc, division.name AS Division, "" AS
Dept, category.name AS Category, brand.name AS Brand, 5400 AS Acct,
(invoice_d!cost*invoice_d!ship_qty)*-1 AS ext_Cost,
invoice_h.invoice_date
FROM category INNER JOIN (brand INNER JOIN (division INNER JOIN (term
INNER JOIN (item INNER JOIN (customer_type INNER JOIN (customer INNER
JOIN (invoice_d INNER JOIN invoice_h ON invoice_d.invoice_id =
invoice_h.invoice_id) ON customer.customer_id = invoice_h.customer_id)
ON customer_type.customer_type_id = customer.customer_type_id) ON
item.item_id = invoice_d.item_id) ON term.term_id = invoice_h.term_id)
ON division.division_id = item.division_id) ON brand.brand_id =
item.brand_id) ON category.category_id = item.master_category_id
WHERE
(((invoice_h.invoice_date)>=[Forms]![Sales_Reports_Form]![STARTDATE]
And (invoice_h.invoice_date)<=[Forms]![Sales_Reports_Form]![ENDDATE])
AND ((invoice_d.price)=0));
union all
SELECT invoice_d.invoice_id, invoice_d.line_id, term.name,
customer_type.name AS CustomerType, customer.name, invoice_d.item_id,
invoice_d.order_qty, item.short_desc, "Corporate" AS Division, "" AS
Dept, "" AS Category, "" AS Brand, 2100 AS Acct,
(invoice_d!cost*invoice_d!ship_qty*0.095)*-1 AS extSalesTax,
invoice_h.invoice_date
FROM category INNER JOIN (brand INNER JOIN (division INNER JOIN (term
INNER JOIN (item INNER JOIN (customer_type INNER JOIN (customer INNER
JOIN (invoice_d INNER JOIN invoice_h ON invoice_d.invoice_id =
invoice_h.invoice_id) ON customer.customer_id = invoice_h.customer_id)
ON customer_type.customer_type_id = customer.customer_type_id) ON
item.item_id = invoice_d.item_id) ON term.term_id = invoice_h.term_id)
ON division.division_id = item.division_id) ON brand.brand_id =
item.brand_id) ON category.category_id = item.master_category_id
WHERE (((term.name)<>"Warranty") AND
((invoice_h.invoice_date)>=[Forms]![Sales_Reports_Form]![STARTDATE]
And (invoice_h.invoice_date)<=[Forms]![Sales_Reports_Form]![ENDDATE])
AND ((invoice_d.price)=0));
union all
SELECT invoice_d.invoice_id, invoice_d.line_id, term.name,
customer_type.name AS CustomerType, customer.name, invoice_d.item_id,
invoice_d.order_qty, item.short_desc, [Promo recode map].Division,
[Promo recode map].Department, "" AS Category, [Promo recode
map].Brand, [Promo recode map].[Sales Tax Acct],
invoice_d!cost*invoice_d!ship_qty*0.095 AS extSalesTax,
invoice_h.invoice_date
FROM category INNER JOIN ([Promo recode map] INNER JOIN (brand INNER
JOIN (division INNER JOIN (term INNER JOIN (item INNER JOIN
(customer_type INNER JOIN (customer INNER JOIN (invoice_d INNER JOIN
invoice_h ON invoice_d.invoice_id = invoice_h.invoice_id) ON
customer.customer_id = invoice_h.customer_id) ON
customer_type.customer_type_id = customer.customer_type_id) ON
item.item_id = invoice_d.item_id) ON term.term_id = invoice_h.term_id)
ON division.division_id = item.division_id) ON brand.brand_id =
item.brand_id) ON [Promo recode map].[Customer id] =
customer.customer_id) ON category.category_id = item.master_category_id
WHERE (((term.name)<>"Warranty") AND
((invoice_h.invoice_date)>=[Forms]![Sales_Reports_Form]![STARTDATE]
And (invoice_h.invoice_date)<=[Forms]![Sales_Reports_Form]![ENDDATE])
AND ((invoice_d.price)=0));
UNION ALL SELECT invoice_d.invoice_id, invoice_d.line_id, term.name,
customer_type.name AS CustomerType, customer.name, invoice_d.item_id,
invoice_d.order_qty, item.short_desc, [Promo recode map].Division AS
Expr1, [Promo recode map].Department AS Expr2, "" AS Category, [Promo
recode map].Brand AS Expr3, [Promo recode map].[Sales Tax Acct] AS
Expr4, (invoice_d!cost*invoice_d!ship_qty)*0.095 AS ext_Cost,
invoice_h.invoice_date
FROM [Promo recode map] INNER JOIN (term INNER JOIN (item INNER JOIN
(customer_type INNER JOIN (customer INNER JOIN (invoice_d INNER JOIN
invoice_h ON invoice_d.invoice_id = invoice_h.invoice_id) ON
customer.customer_id = invoice_h.customer_id) ON
customer_type.customer_type_id = customer.customer_type_id) ON
item.item_id = invoice_d.item_id) ON term.term_id = invoice_h.term_id)
ON [Promo recode map].[Customer type] = customer_type.name
WHERE (((term.name)<>"Warranty") AND ((customer_type.name)="CORE
WHOLESALE" Or (customer_type.name)="EMPLOYEE PURCHASES" Or
(customer_type.name)="FOREIGN DISTRIBUTOR" Or
(customer_type.name)="KEY DISTRIBUTOR" Or (customer_type.name)="KEY
WHOLESALE" Or (customer_type.name)="HOUSE ACCOUNTS" Or
(customer_type.name)="FOREIGN WHOLESALE") AND
((invoice_h.invoice_date)>=[Forms]![Sales_Reports_Form]![STARTDATE]
And (invoice_h.invoice_date)<=[Forms]![Sales_Reports_Form]![ENDDATE])
AND ((invoice_d.price)=0));
---------------------------------------------------------------------------
The query based on that query, which also runs fine.
------------------------------------------------------------------------
SELECT [Promo Recode Union complete].invoice_id, [Promo Recode Union
complete].line_id, [Promo Recode Union complete].term.name, [Promo
Recode Union complete].CustomerType, [Promo Recode Union
complete].customer.name, [Promo Recode Union complete].item_id, [Promo
Recode Union complete].order_qty, [Promo Recode Union
complete].short_desc, [Promo Recode Union complete].Division, [Promo
Recode Union complete].Dept, [Promo Recode Union complete].Category,
[Promo Recode Union complete].Brand, [Promo Recode Union
complete].Acct, [Promo Recode Union complete].ext_Cost, [Promo Recode
Union complete].invoice_date, invoice_h.customer_id, customer.name,
territory.name
FROM territory INNER JOIN (customer INNER JOIN (invoice_h INNER JOIN
[Promo Recode Union complete] ON invoice_h.invoice_id = [Promo Recode
Union complete].invoice_id) ON customer.customer_id =
invoice_h.customer_id) ON territory.territory_id = customer.territory_id
GROUP BY [Promo Recode Union complete].invoice_id, [Promo Recode Union
complete].line_id, [Promo Recode Union complete].term.name, [Promo
Recode Union complete].CustomerType, [Promo Recode Union
complete].customer.name, [Promo Recode Union complete].item_id, [Promo
Recode Union complete].order_qty, [Promo Recode Union
complete].short_desc, [Promo Recode Union complete].Division, [Promo
Recode Union complete].Dept, [Promo Recode Union complete].Category,
[Promo Recode Union complete].Brand, [Promo Recode Union
complete].Acct, [Promo Recode Union complete].ext_Cost, [Promo Recode
Union complete].invoice_date, invoice_h.customer_id, customer.name,
territory.name;
-------------------------------------------------------------------------
The query which fails, which is the exact same as above except for
having LESS fields:
--------------------------------------------------------------------------
SELECT territory.name, [Promo Recode Union complete].Division, [Promo
Recode Union complete].ext_Cost
FROM territory INNER JOIN (customer INNER JOIN (invoice_h INNER JOIN
[Promo Recode Union complete] ON invoice_h.invoice_id = [Promo Recode
Union complete].invoice_id) ON customer.customer_id =
invoice_h.customer_id) ON territory.territory_id = customer.territory_id
GROUP BY territory.name, [Promo Recode Union complete].Division,
[Promo Recode Union complete].ext_Cost;
---------------------------------------------------------------------------
KARL said:
The query that fails, (and the underlying query) has not been touched.
You did not post the underlying query. Data can cause a query to
fail particular a Null when doing math on a field.