总和内的子查询或总和。这是可能的?

时间:2018-10-05 18:57:53

标签: php mysql sql mysqli

我需要一个问题的帮助,要有代码和总和,求和之后,我需要用获得的结果来实现另一总和。

可以对分组和求和后运行查询吗?我的代码:

$start = "2018-08-01";
$end = "2018-08-30";

$this->db->where("DATE(launched_on) BETWEEN DATE('".$start."') AND DATE('".$end."')");

$this->db->select("*, 
    payment_method,sum(value) as Total, 
    SUM(value * (REPLACE(comission, '1/', '') / 100)) as commission_new, 
    GROUP_CONCAT(id separator ';')  as all_ids, 
    SUM(IF(received_employee = 'yes',
    value * (REPLACE(comission, '1/', '') / 100) , 0)) as pay_value, 
    sum(amount) as Total_sale");

$this->db->group_by('order_id, product');

$this->db->where("type_transaction='products'");

$teste = $this->db->get_where('cashier_transaction', 
array('company_id'=>"1", 
    'type_payment'=>'finish_order', 
    'type'=>'revenue', 
    'product'=>'1'))->result();

结果代码:

[0] => stdClass Object
    (
        [amount] => 1
        [order_id] => 11307
        [product] => 1
        [payment_method] => 2
        [Total] => 76
        [commission_new] => 13.68
        [all_ids] => 15242
        [pay_value] => 13.68
        [Total_sale] => 1
    )

[1] => stdClass Object
    (
        [amount] => 1
        [order_id] => 11402
        [product] => 1
        [payment_method] => 3
        [Total] => 76
        [commission_new] => 13.68
        [all_ids] => 15350
        [pay_value] => 13.68
        [Total_sale] => 1
    )

[2] => stdClass Object
    (
        [amount] => 1
        [order_id] => 11536
        [product] => 1
        [payment_method] => 3
        [Total] => 76
        [commission_new] => 13.68
        [all_ids] => 15532
        [pay_value] => 13.68
        [Total_sale] => 1
    )

[3] => stdClass Object
    (
        [amount] => 1
        [order_id] => 11546
        [product] => 1
        [payment_method] => 3
        [Total] => 76
        [commission_new] => 13.68
        [all_ids] => 15549
        [pay_value] => 13.68
        [Total_sale] => 1
    )

[4] => stdClass Object
    (
        [amount] => 1
        [order_id] => 11616
        [product] => 1
        [payment_method] => 2
        [Total] => 76
        [commission_new] => 13.68
        [all_ids] => 15637
        [pay_value] => 13.68
        [Total_sale] => 1
    )

OBS:我需要组order_id,因为存在多种付款方式。

我需要以下结果:(总和为“ Total,comission_new,pay_value和按产品分组的所有值”)

[0] => stdClass Object
    (
        [product] => 1
        [Total] => 380
        [commission_new] => 68.35
        [all_ids] => 15837;15549;15532;15242
        [pay_value] => 68.35
        [Total_sale] => 5
    )

有可能吗?

1 个答案:

答案 0 :(得分:0)

解决方案:

    $start = "2018-07-01";
    $end = "2018-08-02";
    $teste = $this->db->query("select product, 
        SUM(Total) as total, SUM(commission_new) as commission, 
        GROUP_CONCAT(all_ids separator ';')  as all_idss, 
        SUM(Total_sale) as total_sale  from (SELECT amount, 
        SUM(if(installment_total = installment, amount, 0)) as Total_sale, 
        order_id,product, 
        payment_method,
        sum(value) as Total, 
        SUM(value * (REPLACE(comission, '1/', '') / 100)) as commission_new, 
        GROUP_CONCAT(order_id separator ';')  as all_ids, 
        SUM(IF(received_employee = 'yes', value * (REPLACE(comission, '1/', '') / 100) , 0)) as pay_value FROM cashier_transaction 
        WHERE company_id='1' and 
        type_payment='finish_order' and 
        type='revenue' and 
        type_transaction='products' and 
        product='1' and 
        DATE(launched_on) BETWEEN DATE(?) AND DATE(?) 
        GROUP BY order_id, product) 
        p group by product", array($start,$end))->result();
    print_r($teste);

结果:

[0] => stdClass Object
    (
        [product] => 1
        [total] => 4332
        [commission] => 779.7599999999992
        [all_idss] => 4654;4654;4689;4742;4742;4888;4894;5020;5492;5593;5843;6386;6539;6849;6849;6976;6980;6983;7159;7183;7308;7690;7775;7903;7903;7942;7987;8054;8222;8359;8445;8627;8684;8807;9028;9256;9300;9423;9684;9858;9998;9999;9999;10023;10024;10026;10169;10361;10365;10366;10442;10557;10587;10642;10718;11307;11402;11536;11546;11616
        [total_sale] => 57
    )