from anxin.trade.models import *
from django.db.models import *
q1 = DeviceBill.objects.values('pay_type').annotate(total=Sum('money'))
print(q1.query.__str__())
q2 = DeviceBill.objects.order_by('-created_at').values('pay_type').annotate(total=Sum('money'))
print(q2.query.__str__())
q3 = DeviceBill.objects.values('pay_type').annotate(total=Sum('money')).order_by('-created_at')
print(q3.query.__str__())
SELECT "trade_devicebill"."pay_type", SUM("trade_devicebill"."money") AS "total" FROM "trade_devicebill" GROUP BY "trade_devicebill"."pay_type"
SELECT "trade_devicebill"."pay_type", SUM("trade_devicebill"."money") AS "total" FROM "trade_devicebill" GROUP BY "trade_devicebill"."pay_type", "trade_devicebill"."created_at" ORDER BY "trade_devicebill"."created_at" DESC
SELECT "trade_devicebill"."pay_type", SUM("trade_devicebill"."money") AS "total" FROM "trade_devicebill" GROUP BY "trade_devicebill"."pay_type", "trade_devicebill"."created_at" ORDER BY "trade_devicebill"."created_at" DESC
SELECT "trade_devicebill"."pay_type", SUM("trade_devicebill"."money") AS "total"
FROM "trade_devicebill"
GROUP BY "trade_devicebill"."pay_type", "trade_devicebill"."created_at"
ORDER BY "trade_devicebill"."created_at" DESC
DeviceBill model 为
q1 的group by 为 pay_type
q2和q3 的group by 为 pay_type,created_at
在sql中order by字段是必须在group by字段中的