mysql - Multiplying SUM value from one table with a variable from another -


i have 2 tables:

  • t_shipment, contains shipper_account, ship_value, , ship_date (among others, irrelevant

  • t_customer, contains account_number , ship_fee (ship fee percentage charged each shipment, , number varies account) (likewise, table contains other fields well)

    • note: shipper_account refers account_number (though it's not treated foreign key)

i need calculate sum transactions done account in day t_shipment, multiply corresponding ship_fee in t_customer.

then have tried this:

select sum(ship_value * (ship_fee)) calculated_value t_shipment inner join t_customer on shipper_account = account_number ship_fee not null; 

but doesn't right. want make sure sum(cod_value * (cod_fee/100)) part indeed returns sum of each shipper's ship_value multiplied own ship_fee. (e.g., rather being sum of ship_value shippers multiplied sum of ship_fee.

e.g. if had sets of data:

__________________________________________ |shipper_account | ship_value | ship_date | |1000000000      |    240     |(some date)| |1000000000      |     60     |(some date)| |2000000000      |    100     |(some date)| ------------------------------------------- _____________________________ |account_number  | ship_fee | |1000000000      |    0.5   | |2000000000      |    0.1   | ----------------------------- 

the results should be:

  • sum of shipper account 1000000000 (240 + 60) multiplied 0.5 = 150
  • sum of shipper account 2000000000 (100) multiplied 0.1 = 10
  • sum of (150 + 10 = 160)

rather than

  • sum of ship_values (360) multiplied sum of ship_fee (0.6) = 216

so, simplest solution?

hope work

select sum(s.ship_value *c.ship_fee) calculated_value  t_customer c inner join t_shipment s  on c.account_number = s.shipper_account  c.ship_fee not null  group c.account_number 

or

select sum(ship_value * (ship_fee)) calculated_value  t_shipment inner join t_customer  on shipper_account = account_number  ship_fee not null group account_number; 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -