1

各ポリシーの net_insurance の最後の値を取得し、type_money で合計しようとしています

|policies|
 |id|  |policy_num|  |type_money| 
  1      1234            1
  2      5678            1
  3      3444            2
  4      4577            2


|insurances|
 |id|  |policy_id|  |net_insurance|
  1       1             100
  2       1             200 
  3       1             400
  4       2             500
  5       2             600
  6       3             100
  7       4             200

ここで最後の値を取得しようとしています

semi result bedore to do the sum            

 |policy_num|  |type_money|  |last_net_insurance|
   1234          1            400
   5678          1            600
   3444          2            100
   4577          2            200

type_money で合計したい最後の値を取得した後、それが本当に必要なものです

 |type money|    |sum|
   1              1000
   2               300

これを試しましたが、各ポリシーの最後の net_insurance を取得し、合計を行う方法がわかりません

  select * from policies
  inner join insurances ON (insurances.policy_id =policies.id)
  group by policies.id

これが私がこれをやっているページです

http://sqlfiddle.com/#!2/acd8e/3

4

1 に答える 1