Two where condition for same column using group by

前端 未结 2 872
轻奢々
轻奢々 2021-01-28 03:55

I am having two tables, t1, t2. My tables and expected result are given below. My table schema is in sqlfiddle

t1:

id    branch_name
1     branch1
2              


        
2条回答
  •  耶瑟儿~
    2021-01-28 04:33

    I think this is the query you are looking for:

    SELECT t1.branch_name, 
           COALESCE(SUM(send.vqty), 0) AS send, 
           COALESCE(SUM(receive.vqty), 0) AS received 
    FROM t1  
    LEFT JOIN t2 AS send on t1.id = send.VBRNCH  
    LEFT JOIN t2 AS receive on t1.id = receive.VTOBRN 
    GROUP BY t1.branch_name
    

    Demo here

提交回复
热议问题