Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 367173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:42:10+00:00 2026-05-12T13:42:10+00:00

I am using MYSQL5.1,Though I tried to find documentation for this but was unsuccessful,secondly

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:42:10+00:00Added an answer on May 12, 2026 at 1:42 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this is a popular topic, but I've yet to find an
I'm currently learning mysql, so I am very new to this, tried to find
I'm trying to pull data into flash using JSON but i keep getting this
I’ve been muddling through using MySQL for some time, but it’s still not really
I'm using a MySQL database and accessing it through Java. PreparedStatement prep1 = this.connection.prepareStatement(
If you are using php5 and mysql5, is there a substantial advantage to using
Using MySQL syntax, how would I write a query to return the following (I
Am fairly new to using MySQL and a total novice at Perl but am
I would like to apologize if the duplicate of this question exist. i tried
I have tried following some tutorials and I get the concept of how the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.