Как суммировать значения из разных таблиц в POstgresql? - PullRequest
1 голос
/ 21 мая 2019

у меня есть 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   

Когда я использую этот запрос, мой результат в Sum Double.Я не знаю, в чем проблема. Я много пытался решить эту проблему, но не смог. Это таблица, которую я показываю под вопросом.Может кто-нибудь проверить этот запрос.

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 голосов
/ 21 мая 2019

Я сделал Сам

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
0 голосов
/ 21 мая 2019

Поскольку в ваших таблицах слишком много избыточных полей, вам необходимо обеспечить уникальность 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
...