user2601972
user2601972

Reputation: 47

how to get sum of 3 table columns

  CREATE TABLE `table1` (
 `Nameid` int(6) NOT NULL,
  `name` varchar(20) default NULL,
  `amount` double default NULL,
   PRIMARY KEY  (`Nameid`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;


 INSERT INTO `table1` (`Nameid`, `name`, `amount`) VALUES 
(1, 'chan', 2000),
(2, 'john', 3000),
(3, 'james', 2000);

CREATE TABLE `table2` (
`Pid` int(5) NOT NULL auto_increment,
`Nameid` int(5) default NULL,
`product` varchar(20) default NULL,
`price` double default NULL,
PRIMARY KEY  (`Pid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;


INSERT INTO `table2` (`Pid`, `Nameid`, `product`, `price`) VALUES 
(1, 3, 'ghee', 400),
(2, 2, 'dhal', 100),
(3, 1, 'chenna', 150);

CREATE TABLE `table3` (
`Sid` int(5) NOT NULL,
`Nameid` int(5) default NULL,
`expence` double default NULL,
`place` varchar(25) default NULL,
PRIMARY KEY  (`Sid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `table3` (`Sid`, `Nameid`, `expence`, `place`) VALUES 
(1, 2, 280, 'Ny'),
(2, 1, 500, 'At'),
(3, 3, 600, 'ca');

I want output should be like this :

Nameid|name|product|place|amount|price|expence|price + expence|amount - (price +expence) |

2 |john| dhal  | Ny  | 3000 | 100 |   280 |            380|                     2620 |

1 |chan|chenna | At  | 2000 | 150 |   500 |            650|                     1350 |

3 |james| ghee | ca  | 2000 | 400 |   600 |            1000|                    1000 |

Total|------|-----|-----| 7000 |  650 |  1380 |           2030 |                    4970

Upvotes: 0

Views: 81

Answers (2)

Eugen Rieck
Eugen Rieck

Reputation: 65274

You can use a blind SUM() and WITH ROLLUP to achive this:

SELECT
  table1.Nameid AS Nameid,
  table1.name AS Name,
  table2.product AS Product,
  table3.Place AS Place,
  SUM(table1.amount) AS Amount,
  SUM(table2.price) AS Price,
  SUM(table3.expence) AS Expence,
  SUM(table2.price+table3.expence) AS Price_plus_Expence,
  SUM(table1.amount-(table2.price+table3.expence)) AS Amount_minus_Price_plus_Expence
FROM
  table1
  INNER JOIN table2 ON table2.Namid=table1.Nameid
  INNER JOIN table3 ON table3.Namid=table1.Nameid
GROUP BY table1.Nameid
WITH ROLLUP

Edit

There is no way to create 100% of the OQ output in MySQL:

  • Column names my not contain formulas: No workaround possible (and none necessary: You should not use MySQL client as a presentation/formatting layer)
  • The Rollup line will contain NULL for all non-rollup columns. This could be very hackily be solved with something like. IMHO this also boils down to "Don't use MySQL client as presentation layer"

.

SELECT 
  IFNULL(Nameid,'Total'),
  IFNULL(Name, '------'),
  IFNULL(Product,'-----'),
  IFNULL(Place,'-----'),
  Amount, Price, Expence, Price_plus_Expence, Amount_minus_Price_plus_Expence
FROM (
  -- original answer query here
) AS baseview

Upvotes: 1

echo_Me
echo_Me

Reputation: 37233

try this

     select t1.Nameid , t1.Name ,   t2.Product , t3.Place  , t1.amount , t2.Price  , t3.Expence ,t2.price + t3.expence  , t1.amount-(t2.price + t3.expence)
 from table1 t1
 inner join table2 t2 
 on t1.Nameid = t2.Nameid
 inner join table3 t3
 on t1.Nameid = t3.Nameid
 order by t1.Nameid

DEMO HERE

if you have multiple prices and amount per user you can do this

    select t1.Nameid , t1.Name ,   t2.Product , t3.Place  , sum(t1.amount) sumamount, sum(t2.Price) sumprice , sum(t3.Expence) sumexpence,t2.price + t3.expence as priceexpence , t1.amount-(t2.price + t3.expence) as amountMinuspriceexpence
 from table1 t1
 inner join table2 t2 
 on t1.Nameid = t2.Nameid
 inner join table3 t3
 on t1.Nameid = t3.Nameid
 group by t1.Nameid
 order by t1.Nameid

DEMO HERE

EDIT.

for your wished ressult try this

   select t1.Nameid , t1.Name ,   t2.Product , t3.Place  , t1.amount , t2.Price  , t3.Expence ,t2.price + t3.expence as priceexpence , t1.amount-(t2.price + t3.expence) as amountMinuspriceexpence
 from table1 t1
 inner join table2 t2 
 on t1.Nameid = t2.Nameid
 inner join table3 t3
 on t1.Nameid = t3.Nameid
 union all 

 select 'Toatal','--','--','--', sum(t1.amount)as sumamount,sum(t2.price) as sumprice,sum(t3.Expence)as sumexpence, sum(t2.price + t3.expence) as sumpriceexpence ,sum(t1.amount-(t2.price + t3.expence))sumamountminuspriceexpence
 from table1 t1
 inner join table2 t2 
 on t1.Nameid = t2.Nameid
 inner join table3 t3
 on t1.Nameid = t3.Nameid

DEMO HERE

Upvotes: 0

Related Questions