django 实现根据状态统计数据

原生 SQL

SELECT 
SUM(
 CASE  WHEN order_status = 4 THEN deposit_amt - refund_amt + final_amt
    WHEN order_status = 7 THEN deposit_amt 
    WHEN order_status = 0 THEN deposit_amt 
    ELSE 0
  END
 )
 FROM "order" WHERE deleted_at = 0 AND cancelled = FALSE AND boat_type=1;

Djiango

hire = hire.aggregate(
            sum=Sum(
                Case(
                    When(order_status=4, then=F('deposit_amt') - F('refund_amt') + F('final_amt')),
                    When(Q(order_status=7) | Q(order_status=0), then=F('deposit_amt')),
                    default=0
                )
            )
        )
posted @ 2023-10-26 16:17  vx_guanchaoguo0  阅读(15)  评论(0编辑  收藏  举报