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 3460314
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:12:52+00:00 2026-05-18T10:12:52+00:00

I’ve no idea if I am using UNION correctly in this scenario – there

  • 0

I’ve no idea if I am using UNION correctly in this scenario – there may well be a better/easier way and I’m open to suggestions:

I have the following code:

SELECT COUNT(*), AVG (q1) AS q1, AVG (q2) AS q2, AVG (q3) AS q3, AVG (q4) AS q4, AVG (q5) AS q5, AVG (q6) AS q6, AVG (q7) AS q7, AVG (q8) AS q8, AVG (q9) AS q9, AVG (q10) AS q10, AVG (q11) AS q11, AVG (q12) AS q12, AVG (q13) AS q13, AVG (q14) AS q14, AVG (q15) AS q15, AVG (q16) AS q16, AVG (q17) AS q17, AVG (q18) AS q18, AVG (q19) AS q19, AVG (q20) AS q20, AVG (q21) AS q21, AVG (q22) AS q22 FROM thotels_results WHERE brand = 'EFG' AND date = 'NOV2010' GROUP BY brand
UNION
SELECT COUNT(*), AVG (q1) AS q1, AVG (q2) AS q2, AVG (q3) AS q3, AVG (q4) AS q4, AVG (q5) AS q5, AVG (q6) AS q6, AVG (q7) AS q7, AVG (q8) AS q8, AVG (q9) AS q9, AVG (q10) AS q10, AVG (q11) AS q11, AVG (q12) AS q12, AVG (q13) AS q13, AVG (q14) AS q14, AVG (q15) AS q15, AVG (q16) AS q16, AVG (q17) AS q17, AVG (q18) AS q18, AVG (q19) AS q19, AVG (q20) AS q20, AVG (q21) AS q21, AVG (q22) AS q22 FROM thotels_results WHERE brand = 'XYC' AND date = 'NOV2010' GROUP BY brand
UNION
SELECT COUNT(*), AVG (q1) AS q1, AVG (q2) AS q2, AVG (q3) AS q3, AVG (q4) AS q4, AVG (q5) AS q5, AVG (q6) AS q6, AVG (q7) AS q7, AVG (q8) AS q8, AVG (q9) AS q9, AVG (q10) AS q10, AVG (q11) AS q11, AVG (q12) AS q12, AVG (q13) AS q13, AVG (q14) AS q14, AVG (q15) AS q15, AVG (q16) AS q16, AVG (q17) AS q17, AVG (q18) AS q18, AVG (q19) AS q19, AVG (q20) AS q20, AVG (q21) AS q21, AVG (q22) AS q22 FROM thotels_results WHERE brand = 'ABC' AND date = 'NOV2010' GROUP BY brand

It outputs the following:

       q1      q2      q3   etc.                                                                                        
140 8.7714  8.8429  8.1643  8.7500  8.7571  8.9000  9.4071  9.1214  8.5714  8.7643  9.5143  8.9429  9.1643  8.9857  7.9500  8.9286  8.7000  9.0429  9.0143  8.7214  9.1214  9.3071
29   8.1724  8.2414  8.2414  7.8966  8.5862  8.5517  9.0000  8.5862  8.1724  7.9655  8.8966  8.6207  8.2414  8.3793  7.8276  8.3793  7.9310  8.4138  8.6897  8.3448  8.8621  8.5172
897 8.6009  8.5686  7.8528  8.3133  8.3423  8.6410  9.0301  8.6912  8.3233  8.3389  9.2029  8.3969  8.6856  8.5017  7.8071  8.4816  8.3512  8.6789  8.6789  8.3913  8.6388  8.8986

All I would ‘like to do’ is AVERAGE each of the q1, q2, q3 columns or SUM them and divide by 3.

Like I say, if there is a better way that doesn’t use JOIN, that’s fine with me!!!

Thanks in advance,

Homer.

  • 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-18T10:12:52+00:00Added an answer on May 18, 2026 at 10:12 am

    (1) You don’t need to calculate the results separately and then UNION them like that. (2) I think WITH ROLLUP will probably do what you need.

    SELECT COUNT(*), AVG (q1) AS q1,... 
    FROM thotels_results WHERE brand in ('ABC','EFG','XYZ') AND date = 'NOV2010'
    GROUP BY brand
    WITH ROLLUP
    

    But as pointed out in the comments this doesn’t do the “mean-of-mean”s averaging required. The best I could come up with for that is

    CREATE TEMPORARY TABLE results 
    SELECT 
           brand, 
           COUNT(*) AS cnt, 
           AVG (q1) AS q1
           ...
    FROM thotels_results 
    WHERE brand in ('ABC','EFG','XYZ') AND date = 'NOV2010'
    GROUP BY brand;
    
    CREATE TEMPORARY TABLE results2 
    SELECT cast(NULL as char), AVG(cnt), AVG(q1) 
    FROM results r2;
    
    
    /*MySQL doesn't allow the same temp table to be accessed twice in a UNION!*/
    SELECT * FROM results r
    UNION ALL
    SELECT *
    FROM results2;
    
    DROP TEMPORARY TABLE results;
    DROP TEMPORARY TABLE results2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.