tags:

views:

178

answers:

2

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
+3  A: 

The error indicates that your ORDER BY is ambiguous, you need to parenthesize your SELECT statements to indicate to MySQL how it is to be applied.

From the MySQL Docs:

To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one.

So, given your query, put each query inside parentheses and place the ORDER BY outside them:

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)
  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

Alternatively, if you wanted the ORDER BY to apply to each statement individually before the UNION, put the ORDER BY within the parentheses for each of the two SELECT statements.

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
Adam Bellaire
@ Adam ::thanx for the kind reply,does this means order by can be applied only once unlike group by?
Sam Rudolph
@Sam: See my edited response, you can use ORDER BY with each select as well by placing it within the parentheses.
Adam Bellaire
@Adam,ohh thanx for the reply even the query I query i asked has got clear after reading the repective documentation,thanx for the help
Sam Rudolph
@Adam but why We need inner parenthesis??
Sam Rudolph
@Sam: I'm guessing it's because the second ORDER BY is ambiguous, mySQL can't tell whether it applies to the SELECT it follows or if it applies to the entire UNION unless you supply parenthesis. I'm not sure if it cares about the first ORDER BY being inside parentheses or not, but I think using parallel syntax would probably be for the best either way.
Adam Bellaire
@Adam::Thanx for the Link and for Help..it Worked!!
Sam Rudolph
A: 

http://dev.mysql.com/doc/refman/5.1/en/

maybe error is in ", time_dim as time" but I am not sure - try to cut out parts until error is gone and you'll find the error

tig