zoukankan      html  css  js  c++  java
  • Django 分组 聚合

    base_sql = Order.objects.filter(is_paid=True, merchant=merchant_id)
    
    # 如果aggregate前没有values,得到的结果是一个字典

    base_sql.aggregate(amount=Sum("total_amount")).get("amount")
    # 如果有values, aggregate出来的是一个queryset
    base_q = UserCoupon.objects.filter(
            user__merchant=merchant_id,
            created_at__date=date
        )
    sq_1 = base_q.values('coupon_id').annotate(
            # 领用数量
            pickuped_count=Count('id'),
            # 使用数量
            used_count=Count('id', filter=Q(used_at__isnull=False))
        ).all()
    
    # annotate 前必须跟values 代表以xxx分组  最后的filter代表mysql的 havving
    base_sql.values("creator_id").annotate(order_count=Count('id')).filter(order_count__gte=2).count()

    # 反欺诈请求人数 根据user_id去重
    ry_req_user_count = RyReqRecord.objects.filter(created_at__range=(start_date_ime, end_datetime)).
    values('account_id').distinct().count()

      

  • 相关阅读:
    归并排序
    快速排序
    冒泡排序
    排序算法复杂度
    [LeetCode] 20. Valid Parentheses ☆(括号匹配问题)
    makefile编写helloworld
    shell的通俗理解
    PID三种参数的理解
    PID的原理
    PID控制温度
  • 原文地址:https://www.cnblogs.com/shenwenlong/p/9389470.html
Copyright © 2011-2022 走看看