Matrix Multiplication In Python And Mysql
I have a currency exchange dictionary, as follows: exchange_rates = {'USD': 1.00000, 'EUR': 1.32875, 'GBP': 1.56718, ...} Then I retrieve the s
Solution 1:
Instead of getting one million rows from the database and doing the calculation in Python, give your dictionary to the database and get the database to do the calculation and send you back the result.
You can do this by making a query similar to the following:
SELECTSUM(price * exchange_rate) AS total
FROM sales
LEFTJOIN
(
SELECT'USD'AS currency, 1.00000AS exchange_rate
UNIONALLSELECT'EUR', 1.32875UNIONALLSELECT'GBP', 1.56718-- ...
) AS exchange
ON exchange.currency = sales.currency
Solution 2:
Do
SELECT price FROM sales
then sum up all the items you get back, assuming the numbers in your table are in USD.
Solution 3:
SELECT SUM(price * factor)
FROM
(
SELECT sales.price AS price, exchange.factor AS factor
FROM sales
JOIN exchange ON exchange.currency = sales.currency
) AS t
This assumes you have an exchange table
Post a Comment for "Matrix Multiplication In Python And Mysql"