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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:43:06+00:00 2026-05-30T15:43:06+00:00

I have sql query like this: select a.x, a.y, sum(a.foo) as foo_sum from a

  • 0

I have sql query like this:

select a.x, a.y, sum(a.foo) as foo_sum
from a
group by a.x

and after running this query, I can access to foo_sum.

I want to do something like this:

select a.x, a.y, sum(a.foo) as foo_sum
from a
group by a.x
union
select a.x, a.y, sum(a.bar) as bar_sum
from a
group by a.y

but after running this one, my program dosent know foo_sum, anybody knows why?

thank you.

Edit: I want to have 4 column x, y, foo_sum and bar_sum, is this possible?

  • 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-30T15:43:07+00:00Added an answer on May 30, 2026 at 3:43 pm

    I don’t think this is possible, because your number of unique x values might not be the same as the number of unique y values.

    For example, suppose a was like this:

    +------+------+------+------+
    | x    | y    | foo  | bar  |
    +------+------+------+------+
    |    1 |    1 |    3 |    4 |
    |    1 |    2 |    3 |    6 |
    |    1 |    1 |    3 |    6 |
    |    1 |    2 |    8 |    3 |
    |    2 |    1 |    7 |   32 |
    |    2 |    2 |    7 |   34 |
    |    2 |    3 |    8 |    3 |
    +------+------+------+------+
    

    The first query (foo_sum GROUP BY x) would give:

    +------+------+---------+
    | x    | y    | foo_sum |
    +------+------+---------+
    |    1 |    1 |      17 |
    |    2 |    1 |      22 |
    +------+------+---------+
    

    The second (bar_sum GROUP BY y) gives:

    +------+------+---------+
    | x    | y    | bar_sum |
    +------+------+---------+
    |    1 |    1 |      42 |
    |    1 |    2 |      43 |
    |    2 |    3 |       3 |
    +------+------+---------+
    

    How can you mash the bar_sum column on the end of the foo_sum table? There is no corresponding foo_sum value for (x,y)=(2,3), but there is a corresponding bar_sum.

    The best you could achieve would be something like this:

    +------+------+---------+-------+
    | x    | y    | bar_sum |foo_sum|
    +------+------+---------+-------+
    |    1 |    1 |      42 |    17 |
    |    1 |    2 |      43 |  NULL |
    |    2 |    3 |       3 |  NULL |
    |    2 |    1 |    NULL |    22 |
    +------+------+---------+-------+
    

    To achieve this the only way I can think of is using a FULL OUTER JOIN. Note that doing SELECT x, y, SUM(foo), SUM(bar) FROM a GROUP BY x,y won’t give the same results as it groups differently.

    SELECT t1.x,t1.y,foo_sum,bar_sum
    FROM
    (SELECT x, y, SUM(foo) as foo_sum
    FROM A
    GROUP BY x) t1
    FULL OUTER JOIN
    (SELECT x, y, SUM(bar) as bar_sum
    FROM A
    GROUP BY y) t2
    ON t1.x=t2.x AND t1.y=t2.y
    

    This makes sure all the x,y combinations from each table is inserted, even if there are no corresponding x,y in the other table.

    However MySQL has no FULL OUTER JOIN, and you have to simulate a A FULL OUTER JOIN B by UNIONing a LEFT and RIGHT join together:

    SELECT ...
     FROM A LEFT JOIN B ON ...
    UNION
    SELECT ...
     FROM A RIGHT JOIN B ON ...
    

    In your case this translates to the very ugly:

    SELECT t1.x,t1.y,foo_sum,bar_sum
    FROM
    (SELECT x, y, SUM(foo) as foo_sum
    FROM A
    GROUP BY x) t1
    LEFT JOIN                        -- need FULL OUTER JOIN
    (SELECT x, y, SUM(bar) as bar_sum
    FROM A
    GROUP BY y) t2
    ON t1.x=t2.x AND t1.y=t2.y
    
    UNION
    
    SELECT t2.x,t2.y,foo_sum,bar_sum
    FROM
    (SELECT x, y, SUM(foo) as foo_sum
    FROM A
    GROUP BY x) t1
    RIGHT JOIN                        -- need FULL OUTER JOIN
    (SELECT x, y, SUM(bar) as bar_sum
    FROM A
    GROUP BY y) t2
    ON t1.x=t2.x AND t1.y=t2.y
    

    This is highly inefficient ! I suggest you instead do the grouping on the PHP side (or whatever other language you are using with MySQL) than the SQL side.

    Who knows, there may be a more efficient way to do this since all you want is SUMs – there could be a clever way to group and sum to achieve your effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sql query that looks like this: SELECT SUM(A) AS expr1 FROM
I have a MySQL query like this: SELECT *, SUM(...some SQL removed for brevety)
Right now, I have a SQL Query like this one: SELECT X, Y FROM
In T-SQL you could have a query like: SELECT * FROM Users WHERE User_Rights
I have a query that looks like this: SELECT OrganizationName, OrganizationID, ReceivableStatus, InvoiceFee FROM
I have a SQL query like this: SELECT E.HESAP, B.TEKLIF_NO1 + '/' + B.TEKLIF_NO2
I have query in SQL SERVER 2008 like this. And this is working. SELECT
I have a query that looks like this: SELECT SUM(`georder`) AS `order`, SUM(quantity) AS
I've done a query like this : SELECT Code, kind, SUM(valP) AS value FROM
I have this SQL query but its running soo slow, SELECT wr.wr_re_id as ReID,

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.