1

I want to get sum of different columns from different tables, I have done something like this below, I think there is much better way that this in mysql

select 
(select sum(sal) from budget_tp) as sal_tp, 
(select sum(sal) from budget_fos_veri) as sal_veri,
(select sum(sal) from budget_fos_picks)as sal_pick,
(select sum(sal) from budget_bpo_other)as sal_bpo;
5
  • Is it running slowly? What makes you think it can be improved? Commented Aug 3, 2012 at 6:45
  • Are there related columns in each table? Commented Aug 3, 2012 at 6:45
  • Thanks for fast reply. You mean its the best way, actually I am noob. :d Commented Aug 3, 2012 at 6:46
  • No, there are no related columns, tables are independent. Commented Aug 3, 2012 at 6:47
  • Now what if i want two columns from one of the table above as select (select sum(sal)as sal_tp, sum(local_conv) as lc_tp from budget_tp) , (select sum(sal) from budget_fos_veri) as sal_veri, (select sum(sal) from budget_fos_picks)as sal_pick, (select sum(sal) from budget_bpo_other)as sal_bpo; I get this error ERROR 1241 (21000): Operand should contain 1 column(s) Commented Aug 3, 2012 at 7:03

1 Answer 1

1

Depending on the amount "summed" up, you could use something like nightly-jobs (selects) that fill cache-tables - since budget numbers don't need to be 100% live values (anyone saying SAP HANA now, gets a high five to the face with a chair)

For the pure performance your select statement is as good as it gets.

Edit: this was a purely simplyfied approach. You could also do nightly-jobs for the huge datasets and only add the difference added to your tables since the last job summing up.

Sign up to request clarification or add additional context in comments.

4 Comments

Hey what if I have some 50 columns to select from 50 diff tables? Will this method will be still good for that?
using a job at a time no one is using your database to fill "one" cache table that creates one "already summed up" column for the tables building a sum, yes. So you'd had one column for each source of your summing up. Please NOTE: other Databases (eg. oracle, mssql) might be better for those, since they don't rely on external logic to build those caching tables (but offer on board tech to do those)
Now what if i want two columns from one of the table above as select (select sum(sal)as sal_tp, sum(local_conv) as lc_tp from budget_tp) , (select sum(sal) from budget_fos_veri) as sal_veri, (select sum(sal) from budget_fos_picks)as sal_pick, (select sum(sal) from budget_bpo_other)as sal_bpo; I get this error ERROR 1241 (21000): Operand should contain 1 column(s)
Selecting mutlible columns from a "subquery" (what you wanna do) can be done: stackoverflow.com/questions/583954/… like this. But I guess, using two sub-selects on the same table won't hurt much.

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.