1

I want a resultset for this table:

ID  Number_of_posts   Number_of_user
1   100               21
2   23                34

as

   ID  Number_of_posts   Number_of_user   Number_of_posts_AND_Number_of_user
    1   100               21               178
    2   23                34               178
-----------------------------------------------
        123                55

Is it possible to get the sum of two colums as another column/ as output in mysql?

2
  • 1
    Can you verify that you want the entire total on every row (178,178) or per-ID on each row (121,57)? The former seems wrong since it bears no relationship at all to the ID. Commented May 26, 2010 at 15:35
  • 1
    Yeah, I got that. What I'm saying is the 178 figure is not an attribute of the ID. It would make more sense to have that in to totals row at the bottom with the individual ID-based totals on each row. I'm answering the question I think the OP tried to ask. If it turns out I'm wrong then c'est la vie :-) Commented May 26, 2010 at 15:39

6 Answers 6

3

To get cross-tab totals (horizontal and vertical):

select id,
       number_of_posts as p,
       number_of_users as u,
       number_of_posts+number_of_users as p_and_u
    from tbl
union all
select 99999 as id,
       sum(number_of_posts) as p,
       sum(number_of_users) as u,
       sum(number_of_posts+number_of_users) as p_and_u
    from tbl
order by 1

This will give you:

   id     p     u   p_and_u
-----   ---   ---   -------
    1   100    21       121
    2    23    34        57
99999   123    55       178
Sign up to request clarification or add additional context in comments.

3 Comments

That's quite an interesting query, but I have a feeling that's not what the OP is looking for.
Which is why I asked for clarification in the question comments. It makes little sense to have the entire total on each row since it's not an attribute of the key (ID).
thank you very much, I was not expecting this result but this might be what I want.
2

You're complicating your query needlessly and using more memory that you have to. Pull the records in one query, then make another query to get the aggregates.

I know it doesn't answer your question, but it's what you should be doing instead. =)

Comments

1
SELECT  id, number_of_posts, number_of_user,
        (
        SELECT  SUM(number_of_posts + number_of_user)
        FROM    mytable
        )
FROM    mytable

3 Comments

That gives you the total for the whole table on each row, which I don't think is what they wanted.
@Dave: Actually, I think that's what the OP wants
@Dave: could you please look at the sample recordset the @op provided before downvoting?
0
SELECT SUM(Number_of_posts), SUM(Number_of_user) FROM table;

Comments

0
SELECT *, 
       (SELECT SUM(Number_of_posts) + SUM(Number_of_user) FROM TABLE) AS total 
FROM table;

(Edit: Didn't originally notice it was the total total in the last column.)

Comments

-1

Does MySQL support ROLLUP?

SELECT id,
       SUM(Number_of_posts) Number_of_posts,
       SUM(Number_of_user) Number_of_user,
       SUM(Number_of_posts) + SUM(Number_of_user) Number_of_posts_AND_Number_of_user
  FROM table
  GROUP BY ROLLUP(id)

Edit: based on a quick search, in MySQL the last line might be GROUP BY id WITH ROLLUP.

1 Comment

This will return the third sum record-wise, not table-wise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.