如何从POstgresql中的不同表中求和?

时间:2019-05-21 08:55:03

标签: postgresql postgresql-9.5

我有4张桌子

billing_billmanagement
-------------------------

id    | date         | branch_id 
---------------------------------
1     | 2019-03-01   | 1         
2     | 2019-03-02   | 1          
3     | 2019-03-03   | 1       
4     | 2019-03-04   | 1          
6     | 2009-03-05   | 1  

billing_customerproductbill
-------------------------------      
id    | bill_id | product_id | discounted_price | product_qty
-------------------------------------------------------------
1     | 111     | 1          |  500             |  1        
2     | 112     | 2          |  200             |  2  
3     | 112     | 2          |  600             |  1
4     | 113     | 3          |  400             |  1
6     | 113     | 3          |  100             |  1

users_usercommission
-------------------------------      
id    | bill_id | product_id |staff_user_id | comission_amount
----------------------------------------------------------------
1     | 111     | 1          |  001         |  200        
2     | 112     | 2          |  002         |  300
3     | 112     | 2          |  002         |  400
4     | 113     | 3          |  005         |  500
6     | 113     | 3          |  005         |  600

users_staffuser
--------------- 
id     | name  
-------------- 
001    | Ali         
002    | Hsssan   
003    | Farhan   

当我使用此查询时,我的结果加总和。我不知道出了什么问题。我尝试了很多方法来解决此问题,但我没有。这是我显示的表格结构。有人可以检查此查询。

select  au.name, sum(bcp.product_qty), Sum(bcp.discounted_price), 
Sum(uc.commission_amount)
from billing_customerproductbill bcp
inner join users_usercommission uc on bcp.product_id = uc.product_id
and bcp.bill_id = uc.bill_id
inner join billing_billmanagement bb on bcp.bill_id = bb.id
inner join users_staffuser us on uc.staff_user_id = us.id
where bb.date between '2019-03-01' and '2019-03-05'
 and bb.branch_id = '1'
group by au.first_name 
order by 1 ASC

这是我想要的结果

-------------------------------      
Name  | product_qty | discounted_price| commission_amount
-------------------------------------------------------------
Ali   | 1           | 500             |  200                   
Hsssan| 3           | 800             |  700           

2 个答案:

答案 0 :(得分:0)

由于表中有太多冗余字段,因此您需要在join key之前确保join每一步的唯一性,我认为下面的SQL将为您返回正确的结果(顺便说一下,这里的with子句就像SQL中的子查询一样,但是将为我们提供更好更好的代码风格):

   with billing_customerproductbill_sum as (
    select
        bill_id,
        product_id,
        sum(discounted_price) as discounted_price_sum,
        sum(product_qty) as product_qty_sum
    from
        billing_customerproductbill
    group by
        bill_id,
        product_id
    )
    ,users_usercommission_sum as (
    select
        bill_id,
        product_id,
        staff_user_id,
        sum(comission_amount) as comission_amount_sum
    from
        users_usercommission
    group by
        bill_id,
        product_id,
        staff_user_id
    )
    select
        u.name,
        sum(product_qty_sum) as product_qty,
        sum(discounted_price_sum) as discounted_price,
        sum(comission_amount_sum) as comission_amount
    from
        users_staffuser u
    join
        users_usercommission_sum uc on u.id = uc.staff_user_id
    join
        billing_customerproductbill_sum bc on uc.bill_id = bc.bill_id and uc.product_id = bc.product_id
    group by
        u.name

答案 1 :(得分:-1)

  

我完成了自己的事

select concat(au.first_name,' ',au.last_name), sum(c.qty), sum(c.discount), sum(d.comission)
from (select product_id, bill_id, sum(product_qty) as qty,sum(discounted_price) as discount from billing_customerproductbill group by bill_id,product_id ) c
left join (Select product_service_id,staff_user_id, bill_id, sum(commission_amount) as comission from users_usercommission group by staff_user_id,bill_id, product_service_id ) d
on c.product_id = d.product_service_id and c.bill_id = d.bill_id
left join ( Select creation_date, id, branch_id from billing_billmanagement ) b
on b.id = c.bill_id 
left join (Select id, user_id from users_staffuser) us on d.staff_user_id = us.id
left join(Select id, first_name, last_name from auth_user ) au on us.user_id = au.id
where b.branch_id = 1
and b.creation_date between '2019-04-01' and '2019-04-30'  
group by au.first_name, au.last_name