简体   繁体   中英

TSQL - Sum a union query

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

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
SELECT SUM(MyCol) FROM
(
SELECT ... MyCol FROM Table1
UNION ALL
SELECT ... MyCol FROM Table2
)as tbl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM