如何在Laravel中按日期获取过滤查询并仅获取字段总和并将其按日期分组

问题描述

我需要查询日期之间的订单,并按日期(如10月,11月)对它们进行分组。 并获得组中total_price和sales_count的总和。 我不需要查询的详细信息,我只需要每个组的结果。

我希望我的刀片桌看起来像这样:

    $orderGroups = Order::withCount('orderItems')->filtered($request->all())->get()->groupBy(function($val) {
            return Carbon::parse($val->created_at)->format('Y-M');
        });

        $orders = [];
        foreach($orderGroups as $order => $value){

            $orders[$order]['total_price'] = $value->sum('total_price');
            $orders[$order]['total_order_items'] = $value->sum('order_items_count');
            $orders[$order]['avrage_order_items'] = $value->avg('order_items_count');

        }

        return $orders;

我可以这样得到它:

from typing import List
def longest_chain(submatrix: List[int]) -> int:

    """
    Given a list of integers,return the length of the longest chain of 1's
    that start from the beginning.
    You MUST use a while loop for this! We will check.
    >>> longest_chain([1,1,0])
    2
    >>> longest_chain([0,1])
    0
    >>> longest_chain([1,1])
    1
 """
    count = 0
    i = 0
    while submatrix[i] == 1 and i < len(submatrix)-1:
        count += 1
        i += 1
    return count

def largest_rectangle_at_position(matrix: List[List[int]],x: int,y: int) -> int:

    """
  Returns the area of the largest rectangle whose top left corner is at
    position <x>,<y> in <matrix>.



    You MUST make use of <longest_chain> here as you loop through each row
    of the matrix. Do not modify the input matrix.
    >>> case1 = [[1,0],[1,1],0]]

    >>> largest_rectangle_at_position(case1,0)
    4
    >>> largest_rectangle_at_position(case1,2,0)
    5
    >>> largest_rectangle_at_position(case1,2)
    6
    """

我还需要按季节和10天将它们分组。您建议如何处理?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...