I am using MYSQL5.1,Though I tried to find documentation for this but was unsuccessful,secondly I wanted to know logical error in the following query..
SQL QUERY
SELECT date , month , Sum(fact_1) ,
( 2 / Sum(fact_2) ) , 2 FROM( SELECT
time.date, time.month, time.year,
MAX(sales_fact.sell_out_value) as
fact_1, 0 as fact_2 FROM sales_fact,
time_dim as time WHERE
time.id=sales_fact.time_id AND
time.date=”2008-01-01″ GROUP BY
time.date ORDER BY time.year UNION
SELECT time.date, time.month,
time.year, 0 as fact_1,
MAX(sales_target_fact.sell_out_target)
as fact_2 FROM sales_target_fact,
time_dim as time WHERE
time.id=sales_target_fact.time_id AND
time.date=”2008-01-01″ GROUP BY
time.date ORDER BY time.year ) as
Combined_Table GROUP BY date ORDER BY
year
ERROR LINE
- Incorrect usage of UNION and ORDER BY
- Errorcode#1221
The error indicates that your
ORDER BYis ambiguous, you need to parenthesize yourSELECTstatements to indicate to MySQL how it is to be applied.From the MySQL Docs:
So, given your query, put each query inside parentheses and place the
ORDER BYoutside them:Alternatively, if you wanted the
ORDER BYto apply to each statement individually before theUNION, put theORDER BYwithin the parentheses for each of the twoSELECTstatements.