I'm trying to run multiple queries on multiple tables- similar to "select count(*) from TableA where x=1" per table.
What I'd like to do, is get all of the count(*) values that are returned and sum them into a single value...
Any ideas?
The SUM() function sums up all the values in a given column or the values returned by an expression (which could be made up of numbers, column values, or both). It's a good introduction to SQL's aggregate functions, so let's dive right in!
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.
The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.
Not 100% sure what you mean, but maybe:
SELECT (SELECT COUNT(*) FROM tableA)+(SELECT COUNT(*) FROM tableB)
select sum(individual_counts) from
(
select count(*) as individual_counts from TableA where x = 1
union all
select count(*) from TableB where x = 2
....
) as temp_table_name
you normally only need the alias on the first select when using a union.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With