sum of sums of columns

This is a discussion on sum of sums of columns within the MySQL Database forums, part of the Database Forums category; Hi all, I'va many columns in wich I've some amounts. I need to get the total for every ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-12-2007
Bob Bedford
 
Posts: n/a
Default sum of sums of columns

Hi all,

I'va many columns in wich I've some amounts. I need to get the total for
every client with also the total of totals...

my table.

idclient mediumint unsigned not null,
amount1 float(10,2) unsigned,
amount2 float(10,2) unsigned,
amount3 float(10,2) unsigned;

now I do this: select sum(amount1) as total1, sum(amount2) as total2,
sum(amount3) as total3 from amounts group by idclient.

and I need also this: sum(amount1+amount2+amount3) as total

How can I do in MySQL ?

Thanks for help.

Bob



Reply With Quote
  #2 (permalink)  
Old 03-12-2007
Bob Bedford
 
Posts: n/a
Default Re: sum of sums of columns

> I'va many columns in wich I've some amounts. I need to get the total for
> every client with also the total of totals...
>
> my table.
>
> idclient mediumint unsigned not null,
> amount1 float(10,2) unsigned,
> amount2 float(10,2) unsigned,
> amount3 float(10,2) unsigned;
>
> now I do this: select sum(amount1) as total1, sum(amount2) as total2,
> sum(amount3) as total3 from amounts group by idclient.
>
> and I need also this: sum(amount1+amount2+amount3) as total

This does actually work but if you have one value = null then the result is
null...that's why it didn't work since the beginning for me.

Cheers



Reply With Quote
  #3 (permalink)  
Old 03-12-2007
Luuk
 
Posts: n/a
Default Re: sum of sums of columns


"Bob Bedford" <bob@bedford.com> schreef in bericht
news:45f59403$0$3817$5402220f@news.sunrise.ch...
>> I'va many columns in wich I've some amounts. I need to get the total for
>> every client with also the total of totals...
>>
>> my table.
>>
>> idclient mediumint unsigned not null,
>> amount1 float(10,2) unsigned,
>> amount2 float(10,2) unsigned,
>> amount3 float(10,2) unsigned;
>>
>> now I do this: select sum(amount1) as total1, sum(amount2) as total2,
>> sum(amount3) as total3 from amounts group by idclient.
>>
>> and I need also this: sum(amount1+amount2+amount3) as total

> This does actually work but if you have one value = null then the result
> is null...that's why it didn't work since the beginning for me.
>
> Cheers
>
>


select sum(amount1) as total1, sum(amount2) as total2, sum(amount3) as
total3 , sum(IFNULL(amount1,0)+IFFNULL(amount2,0)+IFNULL(am ount3,0)) as
total4 from amounts group by idclient.


see: http://dev.mysql.com/doc/refman/4.1/...functions.html




Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:34 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0