2列的差异

时间:2016-06-24 14:01:21

标签: difference

我有2个表..我需要比较2表并找到不符合标准的记录

表1

 Order   Price
  123     100
  456     200
  789     150

表2

 Order  Item   Price
  123    1     10
  123    2     90
  123    3     10
  456    1     150
  789    1     100
  789    2     100

结果应为

Order   Item   Price  Diff
  123    1     10    90( 100-10) 100 is from table 1
  123    2     90    0( 90-90)   90 is freq rec diff
  123    3     10     -10
  456    1     150   0(150-150)  150 is from table 1
  789    1     100   50(150-100)  150 is from table 1
  789    2     100   -50(50 -100)  50 is from prev diff

由于

1 个答案:

答案 0 :(得分:-1)

   SELECT
      t2.*,
      t1.price - t2.price
   FROM
      t2
   JOIN
      t1 ON
         t1.order = t2.order