简体   繁体   中英

How to sum total row each month group by month and user name

I have a sales record table for every month. Every salesman have their own code bd and nk in this case.

MySQL

select sale_plc
      , month(sale_date) as mon
      , sum(sale_cost) as cost 
from daily_sales 
 where year(sale_date)='2016' 
group 
    by year(sale_date)
      , month(sale_date)
      , sale_plc 
order 
    by month(sale_date)
      , sale_plc asc 
 LIMIT 0, 30

After the query I've got this result:

id  mon cost
bd  1   224787
nk  1   721102
bd  2   440399
nk  2   898020
bd  3   363543
nk  3   878250

While

  • id =salesman code(sale_plc)
  • mon =month number
  • cost =total sale of this month from this ID.

According to the result. I expect the result with array like this. Array

(
    [1] => Array
        (
            [mon] => 1
            [bd] => 224787
            [nk] => 721102
        )
    [2] => Array
        (
            [mon] => 2
            [bd] => 440399
            [nk] => 898020
        )
    [3] => Array
        (
            [mon] => 3
            [bd] => 363543
            [nk] => 878250
        )
    )

I know that it is something to do with the sale_plc . I need to make it array but I don't have an idea to do so.

I don't have you SQL output, so i create an array and make my own code with the same functionality as i do for your array.

PHP

$arr = array(array("id" => "bd", "mon" => "1", "cost" => "224787"),
            array("id" => "nk", "mon" => "1", "cost" => "721102"),
            array("id" => "bd", "mon" => "2", "cost" => "440399"),
            array("id" => "nk", "mon" => "2", "cost" => "898020"),
            array("id" => "bd", "mon" => "3", "cost" => "363543"),
            array("id" => "nk", "mon" => "3", "cost" => "878250"),
            );

$output_arr = array();      
$tmp = 0;
foreach($arr as $key => $value){
    if($tmp == 0 || $tmp != $value['mon'])
        $output_arr[$value['mon']][mon] = $value['mon'];
    if($value['id'] == 'bd')
        $output_arr[$value['mon']][$value['id']] = $value['cost'];
    if($value['id'] == 'nk')
        $output_arr[$value['mon']][$value['id']] = $value['cost'];
    $tmp = $value['mon'];
}
echo "<pre>";
print_r($output_arr);
echo "</pre>";

Output

Array
(
    [1] => Array
        (
            [mon] => 1
            [bd] => 224787
            [nk] => 721102
        )

    [2] => Array
        (
            [mon] => 2
            [bd] => 440399
            [nk] => 898020
        )

    [3] => Array
        (
            [mon] => 3
            [bd] => 363543
            [nk] => 878250
        )

)

I have already answer With your SQL Output. Given the example with your SQL.

PHP

$output_arr = array();
//Your sql
$sql = "select sale_plc
      , month(sale_date) as mon
      , sum(sale_cost) as cost 
from daily_sales 
 where year(sale_date)='2016' 
group 
    by year(sale_date)
      , month(sale_date)
      , sale_plc 
order 
    by month(sale_date)
      , sale_plc asc 
 LIMIT 0, 30";
$tmp = 0;
$i = 0;
$qry = mysqli_query($conn, $sql);
while ($obj = mysqli_fetch_object($qry )){
    if($tmp == 0 || $tmp != $obj->mon)
        $output_arr[$obj->mon][mon] = $obj->mon;
    if($obj->id == 'bd')
        $output_arr[$obj->mon][$obj->id] = $obj->cost;
    if($obj->id == 'nk')
        $output_arr[$obj->mon][$obj->id] = $obj->cost;
    $tmp = $obj->mon;
}

print_r($output_arr);

Output:

Array(
[1] => Array
    (
        [mon] => 1
        [bd] => 224787
        [nk] => 721102
    )
[2] => Array
    (
        [mon] => 2
        [bd] => 440399
        [nk] => 898020
    )
[3] => Array
    (
        [mon] => 3
        [bd] => 363543
        [nk] => 878250
    )
)

Try this answer, if any problem then please let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM