Skip to content
Advertisement

Why I am getting multiple rows instead of single row of a particular date from database while I am trying to get sum of revenue as Date Wise

I am trying to get a table which sum the total revenue of a particular date, but using the following code I am getting multiple row each date instead of single row: –

$date = $row['date'];

$t_fee = mysql_query("SELECT sum(labs.test_fee), sum(labs.discount), sum(labs.received) from labs join patients on labs.p_id = patients.p_id where date = '$date'");


while($row = mysql_fetch_array($t_fee)) {

    
    $fee_sum = $row[0];
    $discount_sum = $row[1];
    $received = $row[2];
    $after_discount = $fee_sum - $discount_sum;
    $balance = $after_discount - $received;

-----------------------------
Date        | Total Revenue |
-----------------------------
2017-05-23  |   1,000       |
2017-05-19  |   4,190       |
2017-05-19  |   4,190       |
2017-05-19  |   4,190       |
2017-05-18  |   1,350       |
2017-05-08  |   690         |
2017-05-02  |   2,280       |
-----------------------------
Grand Total |   9,510       |
-----------------------------

I want to get results like this: –

-----------------------------
Date        | Total Revenue |
-----------------------------
2017-05-23  |   1,000       |
2017-05-19  |   4,190       |
2017-05-18  |   1,350       |
2017-05-08  |   690         |
2017-05-02  |   2,280       |
-----------------------------
Grand Total |   9,510       |
-----------------------------

Advertisement

Answer

 where date = '$date' 
 group by date;

you need to group by date so append group by date; in the query.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement