Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL - Sum a union query

Tags:

I have a union all query in a stored procedure.

WHat I would like to do is Sum a column and return that query to the client

How would I do this?

Malcolm

like image 626
Malcolm Avatar asked Mar 03 '09 05:03

Malcolm


People also ask

How do you get the total count in UNION query?

Here is the query to count on the union query. mysql> select count(*) as UnionCount from -> ( -> select distinct UserId from union_Table1 -> union -> select distinct UserId from union_Table2 -> )tbl1; The following is the output displaying the count.

How do I sum data in SQL query?

If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; If you need to arrange the data into groups, then you can use the GROUP BY clause.


1 Answers

SELECT     othercol1, othercol2,     SUM(bar) FROM     (     SELECT        othercol1, othercol2, bar     FROM        RT     UNION ALL     SELECT        othercol1, othercol2, bar     FROM        FM     ) foo GROUP BY     othercol1, othercol2 
like image 151
gbn Avatar answered Oct 02 '22 11:10

gbn