Error: "The expression is typed incorrectly or it is too complex..." on a UNION
I have a query:
SELECT 'NA_List_Price' AS PriceList, prices.ModelNumber, IIf(prices.NA_List_Price is Null,-1,IIf(prices.NA_List_Price = "N/A",-2,IIF(Left(prices.NA_List_Price,4)="Call",-3,prices.NA_List_Price))) AS Price, [Select Start mm/dd/yyyy] AS StartDate, '' AS EndDate
FROM prices
UNION
SELECT 'NA_Disti_Price' AS PriceList, prices.ModelNumber, IIf(prices.NA_Disti_Price is Null,-1,IIf(prices.NA_Disti_Price = "N/A",-2,IIF(Left(prices.NA_Disti_Price,4)="Call",-3,prices.NA_Disti_Price))) AS Price, [Select Start mm/dd/yyyy] AS StartDate, '' AS EndDate
FROM prices
that gives the Expression is typed incorrectly or is too complex... error.
If the query has one or the other SELECT, it works fine.
If using the first SELECT, I get prompted for a date (enter 01/11/2012) and the results are correct:
PriceList ModelNumber Price StartDate EndDate
NA_List_Price 1-00664-01 650 01/11/2011
NA_List_Price 1-00827-01 125 01/11/2011
NA_List_Price 1-00827-02 150 01/11/2011
The second SELECT also works correctly returning:
PriceList ModelNumber Price StartDate EndDate
NA_Disti_Price 1-00664-01 455 01/11/2012
NA_Disti_Price 1-00827-01 88 01/11/2012
NA_Disti_Price 1-00827-02 105 01/11/2012
The source Prices table looks like this:
ModelNumber NA_List_Price NA_Disti_Price
1-00664-01 650 455
1-00827-01 125 88
1-00827-02 150 105
It's when I UNION the two to attempt to get a listing like this:
PriceList ModelNumber Price StartDate EndDate
NA_List_Price 1-00664-01 650 01/11/2011
NA_List_Price 1-00827-01 125 01/11/2011
NA_List_Price 1-00827-02 150 01/11/2011
NA_Disti_Price 1-00664-01 455 01/11/2012
NA_Disti_Price 1-00827-01 88 01/11/2012
NA_Disti_Price 1-00827-02 105 01/11/2012
that the error occurs.
A bit of background about the IIf conditions, if that helps. The price fields in the database are defined as Text, not Number fields because some prices may be null, "N/A" or "Call for quote". The system I want to import the listing to does not allow text values as prices, so it uses -1 for null, -2 for "N/A" and -3 any time the source price starts with "Call." So the IIf statements do that conversion, and if the source price is not null, not "N/A" or does not begin with "Call" then it uses the actual, real price value.
Why do the queries work separately but not when combined with a UNION?
Any ideas or alternative solutions to get the output I need will be appreciated. Thanks!
Gary
I have a query:
SELECT 'NA_List_Price' AS PriceList, prices.ModelNumber, IIf(prices.NA_List_Price is Null,-1,IIf(prices.NA_List_Price = "N/A",-2,IIF(Left(prices.NA_List_Price,4)="Call",-3,prices.NA_List_Price))) AS Price, [Select Start mm/dd/yyyy] AS StartDate, '' AS EndDate
FROM prices
UNION
SELECT 'NA_Disti_Price' AS PriceList, prices.ModelNumber, IIf(prices.NA_Disti_Price is Null,-1,IIf(prices.NA_Disti_Price = "N/A",-2,IIF(Left(prices.NA_Disti_Price,4)="Call",-3,prices.NA_Disti_Price))) AS Price, [Select Start mm/dd/yyyy] AS StartDate, '' AS EndDate
FROM prices
that gives the Expression is typed incorrectly or is too complex... error.
If the query has one or the other SELECT, it works fine.
If using the first SELECT, I get prompted for a date (enter 01/11/2012) and the results are correct:
PriceList ModelNumber Price StartDate EndDate
NA_List_Price 1-00664-01 650 01/11/2011
NA_List_Price 1-00827-01 125 01/11/2011
NA_List_Price 1-00827-02 150 01/11/2011
The second SELECT also works correctly returning:
PriceList ModelNumber Price StartDate EndDate
NA_Disti_Price 1-00664-01 455 01/11/2012
NA_Disti_Price 1-00827-01 88 01/11/2012
NA_Disti_Price 1-00827-02 105 01/11/2012
The source Prices table looks like this:
ModelNumber NA_List_Price NA_Disti_Price
1-00664-01 650 455
1-00827-01 125 88
1-00827-02 150 105
It's when I UNION the two to attempt to get a listing like this:
PriceList ModelNumber Price StartDate EndDate
NA_List_Price 1-00664-01 650 01/11/2011
NA_List_Price 1-00827-01 125 01/11/2011
NA_List_Price 1-00827-02 150 01/11/2011
NA_Disti_Price 1-00664-01 455 01/11/2012
NA_Disti_Price 1-00827-01 88 01/11/2012
NA_Disti_Price 1-00827-02 105 01/11/2012
that the error occurs.
A bit of background about the IIf conditions, if that helps. The price fields in the database are defined as Text, not Number fields because some prices may be null, "N/A" or "Call for quote". The system I want to import the listing to does not allow text values as prices, so it uses -1 for null, -2 for "N/A" and -3 any time the source price starts with "Call." So the IIf statements do that conversion, and if the source price is not null, not "N/A" or does not begin with "Call" then it uses the actual, real price value.
Why do the queries work separately but not when combined with a UNION?
Any ideas or alternative solutions to get the output I need will be appreciated. Thanks!
Gary
Last edited: