如何在DjANGO中总结两个或多个行的相同ID?

时间:2017-11-16 05:12:36

标签: django python-2.7 postgresql

Bill# | Product | Quantity | price1 | price2 | total
---------------------------------------------------
PK-20 | prod    | 2        | 1110   | 3700     | 2590    
PK-20 | prod    | 2        | 550    | 3700     | 3145    
PK-21 | prod    | 3        | 550    | 3700     | 3145    
PK-21 | prod    | 3        | 550    | 3700     | 3145

我需要总列总和相同ID的SUM。我的查询是。

filtered_record = BillManagement.objects.filter(        
    creation_date__range=[date, date1], **kwargs
).annotate(
    price=F('product__cost') - F('customerproductbill__discounted_price')
).annotate(total_spent=Sum('price')
).values_list(
    'bill_number', 
    'product__product_name', 
    'quantity',
    'distype__percentage_discount', 
    'customerproductbill__discounted_price',     
    'product__cost', 
    'price', 
    'customerservicebill__discounted_price',
    'total_spent'
).distinct()

1 个答案:

答案 0 :(得分:0)

这是答案

SELECT DISTINCT "billing_billmanagement"."creation_date",
                    "billing_billmanagement"."bill_number",
                    "auth_user"."first_name",
                    "billing_billmanagement"."quantity",
                    SUM(DISTINCT("billing_customerproductbill"."discounted_price")),
                     array_agg(distinct(CASE 
                      WHEN "inventory_product"."product_name" = "product_name"
                    THEN "product_name"
                      ELSE 'NULL'
                      END))
                      AS "product",
                      array_agg(distinct(CASE 
                      WHEN "billing_customerproductbill"."bill_id" = "billing_customerproductbill"."bill_id"
                      THEN "billing_customerproductbill"."discounted_price"
                      END))
                      AS "Product_Discount",
                      array_agg(distinct(CASE
                     WHEN "services_service"."name" = "name"
                    THEN "name"
                      ELSE 'NULL'
                     END)) AS "service",
                      array_agg(distinct(CASE 
                      WHEN "billing_customerservicebill"."bill_id" = "billing_customerservicebill"."bill_id"
                       THEN "billing_customerservicebill"."discounted_price"
                       END))
                       AS "SERVICE_DISCOUNT",
                        SUM(DISTINCT("billing_customerservicebill"."discounted_price")),
                       "billing_billmanagement"."total_amount",
                       "billing_customer"."first_name"
    FROM "billing_billmanagement"
    INNER JOIN "users_staffuser" ON ("billing_billmanagement"."staff_id" = "users_staffuser"."id")
    INNER JOIN "auth_user" ON ("users_staffuser"."user_id" = "auth_user"."id")
    LEFT OUTER JOIN "billing_customerproductbill" ON ("billing_billmanagement"."id" = "billing_customerproductbill"."bill_id")
    LEFT OUTER JOIN "inventory_product" ON ("billing_customerproductbill"."product_id" = "inventory_product"."id")
    LEFT OUTER JOIN "billing_customerservicebill" ON ("billing_billmanagement"."id" = "billing_customerservicebill"."bill_id")
    LEFT OUTER JOIN "services_service" ON ("billing_customerservicebill"."service_id" = "services_service"."id")
    LEFT OUTER JOIN "billing_customer" ON ("billing_billmanagement"."customer_id" = "billing_customer"."id")
    WHERE "billing_billmanagement"."creation_date" BETWEEN '2017-11-01' AND '2017-12-02'
    group by billing_billmanagement.creation_date,billing_billmanagement.bill_number, auth_user.first_name, billing_billmanagement.quantity,
             billing_billmanagement.total_amount,billing_customer.first_name
    ORDER BY "billing_billmanagement"."creation_date" ASC