MySQL id和相应的序列连接

时间:2014-07-31 22:50:37

标签: mysql join

我有2个表,它们使用相同的两个字段,一个ID号和一个序列号。表1(订单表)保存项目的价值,表格2(调整表格)保存相关的任何调整(税,折扣等)。所以我需要进入报告,ID,相应的序列,个别值和调整。 就在我编写查询时,我无法根据订单ID和相应的序列ID来区分值

这是我的查询错误。

select Adjustments, OT.*
from order_table OT
join (select AT2.order_sequence, SUM(AT2.value) as Adjustments, AT2.id
  from adjustment_table AT2
  group by AT2.id
) AT on AT.id = OT.id

Order Table

ID    |ORDER_SEQUENCE |PRICE    
10001 |01             |20
10001 |02             |30
10001 |03             |40
10002 |01             |20
10002 |02             |10
10003 |01             |25
10004 |01             |35
10005 |01             |40
10005 |02             |20
10005 |03             |10
10005 |04             |30
10006 |01             |20


Adjustment Table

ID    |ORDER_SEQUENCE|VALUE   |ADJUSTMENT_TYPE
10001 |01            |2       |Tax
10001 |01            |-5      |Discount
10001 |01            |-.5     |Post Discount Tax Adjustment
10001 |02            |3       |Tax
10001 |03            |4       |Tax
10001 |03            |-10     |Discount
10001 |03            |-1      |Post Discount Tax Adjustment
10002 |01            |2       |Tax
10002 |02            |1       |Tax
10003 |01            |2.5     |Tax
10003 |01            |-6.25   |Discount
10003 |01            |-.63    |Post Discount Tax Adjustment
10004 |01            |3.5     |Tax
10004 |01            |-8.75   |Discount
10004 |01            |-.88    |Post Discount Tax Adjustment
10005 |01            |4       |Tax
10005 |01            |-10     |Discount
10005 |01            |-1      |Post Discount Tax Adjustment
10005 |02            |2       |Tax
10005 |02            |-5      |Discount
10005 |02            |-.5     |Post Discount Tax Adjustment
10005 |03            |1       |Tax
10005 |04            |3       |Tax
10006 |01            |2       |Tax


Results I need
ID    |ORDER_SEQUENCE |PRICE    |Total Adjustments
10001 |01             |20       |-3.5
10001 |02             |30       |3
10001 |03             |40       |-7
10002 |01             |20       |2
10002 |02             |10       |1
10003 |01             |25       |-4.38
10004 |01             |35       |-6.13
10005 |01             |40       |-7
10005 |02             |20       |-3.50
10005 |03             |10       |1
10005 |04             |30       |3
10006 |01             |20       |2

2 个答案:

答案 0 :(得分:0)

如果没有子查询,您应该获得更好的性能:

   SELECT ot.id, 
          ot.order_sequence,
          ot.price,
          COALESCE(SUM(at.value),0) adjustments
     FROM order_table ot
LEFT JOIN adjustment_table at
       ON at.id = ot.id
      AND at.order_sequence = ot.order_sequence
 GROUP BY ot.id, ot.order_sequence, ot.price
 ORDER BY ot.id, ot.order_sequence

如果没有对该组合进行调整,我使用了LEFT JOINCOALESCE来调整0

您甚至可以将总数包括在内:

   SELECT ot.id, 
          ot.order_sequence,
          ot.price,
          COALESCE(SUM(at.value),0) adjustments,
          ot.price + COALESCE(SUM(at.value),0) total
     FROM order_table ot
LEFT JOIN adjustment_table at
       ON at.id = ot.id
      AND at.order_sequence = ot.order_sequence
 GROUP BY ot.id, ot.order_sequence, ot.price
 ORDER BY ot.id, ot.order_sequence

答案 1 :(得分:0)

您只需要将order_sequence添加到分组和加入条件中。

select Adjustments, OT.*
from order_table OT
join (select AT2.order_sequence, SUM(AT2.value) as Adjustments, AT2.id
    from adjustment_table AT2
    group by AT2.id, AT2.order_sequence) AT 
on AT.id = OT.id AND AT.order_sequence = OT.order_sequence
order by OT.id, OT.order_sequence

DEMO