3 Replies - 133 Views - Last Post: 17 September 2013 - 11:48 AM

#1 dannybarh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 19-February 11

How to sum() up multiple occurring values separately

Posted 17 September 2013 - 11:02 AM

Am building a vehicle fleet management system, srevice cost is added each time a vehicle is serviced.

Now, lets say vehicle A have been service 8 times, B 5 times, C 10 times ...
I want to show the SUM(cost) for servicing each vehicle at the same time.

I wrote this, but this,but it only select the first service cost of each row like i want it, BUT IT IS NOT SUMING THEM UP

$accra = mysql_query("SELECT  *, SUM(service_price) FROM serv_entry GROUP BY svreg"); 
 while($row = mysql_fetch_assoc($accra)) 
       {
        $id = $row['id'];
        $service_price = $row['service_price'];
        $receipt = $row['receipt'];
        $svreg = $row['svreg'];
        echo "
        <table align='left' class='table table-striped table-bordered'>
        <tbody>
        <tr>
        <td width='30%'>$svreg</td>
        <td width='30%'>$currency $service_price</td>
        <td width='30%'>$receipt</td>
        </tr>
        </tbody>
        </table>
        ";
       } 


Is This A Good Question/Topic? 0
  • +

Replies To: How to sum() up multiple occurring values separately

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: How to sum() up multiple occurring values separately

Posted 17 September 2013 - 11:10 AM

itís simple, you donít fetch the sum (you fetch the column service_price). Iíd try aliasing the sum so you can fetch it like youíre used for a regular column.
SELECT svreg, SUM(service_price) AS service_sum FROM serv_entry GROUP BY svreg
(since you do a GROUP BY, all the other fields will yield to the first value per svreg)
Was This Post Helpful? 1
  • +
  • -

#3 dannybarh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 19-February 11

Re: How to sum() up multiple occurring values separately

Posted 17 September 2013 - 11:47 AM

Thanks, it worked just fine...
how can i mark this post as SOLVED???
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: How to sum() up multiple occurring values separately

Posted 17 September 2013 - 11:48 AM

leave it as it is. everyone who can read should get the message.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1