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

Posted

技术标签:

【中文标题】子查询或 sum 内的 sum。这是可能的?【英文标题】:Subquery or sum inside sum. it's possible? 【发布时间】:2018-10-05 18:57:52 【问题描述】:

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

是否可以在 grouping 和 sum 之后运行查询?我的代码:

$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:我需要 group 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
    )

有可能吗?

【问题讨论】:

只是好奇...您认为"$start"DATE('".$start."') 之间的区别是什么?...因为一个肯定比另一个打字少很多! 【参考方案1】:

解决方案:

    $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
    )

【讨论】:

以上是关于子查询或 sum 内的 sum。这是可能的?的主要内容,如果未能解决你的问题,请参考以下文章

Postgresql - 具有相关子查询的条件 SUM

在 postgresql 中使用 MIN, MAX, (SUM/COUNT) 子查询

如何使用子查询中的 SUM(...) 列将“行位置”添加到查询中?

访问子查询

多个 SUM 的 MySQL 子查询

使用包含 SUM() 字段的子查询在 MS-Access 中编辑查询