用前缀和sum连接表

时间:2016-02-13 12:28:52

标签: mysql sql join group-by

我有两个mysql表fk_orders& fk_payments。订单表列order_item id没有前缀'OI:'需要将其与order_item_id与'OI:'前缀的付款表相关联,需要汇总结算值+退款并按order_item_id与前缀

分组

fk_orders

|order_item_id |order_id    |Invoice_No       |Invoice_No_Amt  |Qty   |Refund_Qty |Refund_Amount
------------------------------------------------------------------------------------------------
|1131231       |123         |F08OTTN16-1      |100            |1     |            |
|1113138       |321         |F08OTTN16-2      |200            |2     |1           |200
|1231231       |023         |F08OTTN16-3      |100            |1     |1           |100
|1133138       |320         |F08OTTN16-4      |200            |2     |            |
|1134231       |103         |F08OTTN16-5      |100            |1     |            |
|1113538       |300         |F08OTTN16-6      |200            |2     |            |
|1003538       |300         |F08OTTN16-7      |200            |2     |            |

fk_payments

|order_item_id    |order_id    |Invoice_No       |Invoice_No_Amt |Settlement_Value
-----------------------------------------------------------------------------------
|OI:1131231       |123         |F08OTTN16-1      |100            |40
|OI:1113138       |321         |F08OTTN16-2      |200            |150
|OI:1231231       |023         |F08OTTN16-3      |100            |-50
|OI:1133138       |320         |F08OTTN16-4      |200            |200
|OI:1134231       |103         |F08OTTN16-5      |100            |40
|OI:1113538       |300         |F08OTTN16-6      |200            |250
|OI:1131231       |123         |F08OTTN16-1      |100            |40
|OI:1133138       |320         |F08OTTN16-4      |200            |100
|OI:1113138       |321         |F08OTTN16-2      |200            |-200

查询结果如下所示

|order_item_id    |order_id    |Invoice_No       |Invoice_No_Amt |Qty   |Final_Settled_Amt 
                                                                         (refund_amount + 
                                                                         (sum of settled value group by order_item_id)
---------------------------------------------------------------------------------------
|OI:1131231       |123         |F08OTTN16-1      |100            |1     |80
|OI:1113138       |321         |F08OTTN16-2      |200            |2     |150
|OI:1231231       |023         |F08OTTN16-3      |100            |1     |50
|OI:1133138       |320         |F08OTTN16-4      |200            |2     |300
|OI:1134231       |103         |F08OTTN16-5      |100            |1     |400
|OI:1113538       |300         |F08OTTN16-6      |200            |2     |250
|OI:1003538       |300         |F08OTTN16-7      |200            |2     |0 

1 个答案:

答案 0 :(得分:0)

这看起来像invoice_no上的联接和一些聚合。我会这样做:

select o.*,
       (coalesce(Refund_Amount, 0) + coalesce(sv, 0)) as SettledAmount,
       (Invoice_No_Amt - coalesce(Refund_Amount, 0) - coalesce(sv, 0)) as netAmount
from fk_orders o left join
     (select invoice_no, sum(Settlement_Value) as sv
      from fk_payments
      group by invoice_no
     ) p
     on o.invoice_no = p.invoice_no;