2

I have 2 tables :

'pay'

|id_tax|value|id_currency|
--------------------------
|1     |100  |12         |
|1     |200  |12         |
|1     |300  |13         |
|2     |200  |12         |
|2     |200  |12         |
--------------------------

'rate'

|id_currency|rate|
------------------
|12         |1   |
|13         |6,12|
------------------

What SQL query will result in this?

|id_tax|value               |
-----------------------------
|1     |100*1+200*1+300*6,12|
|2     |200*1+200*1         |
-----------------------------

2 Answers 2

2
SELECT  id_tax, SUM(value * rate)
FROM    pay p
JOIN    rate r
ON      r.id_currency = p.id_currency
GROUP BY
        id_tax
Sign up to request clarification or add additional context in comments.

Comments

1

In MySQL, you can calculate it using session variables:

SET @s = 0; 

SELECT  id_tax, @s := s +(value * rate)
FROM    pay p
JOIN    rate r
ON      r.id_currency = p.id_currency
GROUP BY
   p.id_tax

or in a pure set-based but less efficient way:

SELECT  pay.id_currency, pay.val, SUM(pay.value * rate.rate)
FROM    pay
JOIN    rate
ON      rate.id_currency = pay.id_currency
GROUP BY
        pay.id_tax

1 Comment

He wants plain sums, not cumulative. Also, when copying my answer from here: stackoverflow.com/questions/5462205/… , you could, I don't know, at least change wording, or something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.