0

I hope you can help me, in this sql query. (SQL SERVER) I have these tables

TABLE1
COLUMN0 COLUMN1 COLUMN2 COLUMN3
A        DOLAR    2.5   18/02/2013
A        DOLAR    1.5   18/02/2013
B        EURO     22    19/02/2013
B        DOLAR    4.5   19/02/2013
B        DOLAR    1.3   18/02/2013
C        EURO     0.5   19/02/2013

TAB_RATES
COLUMN0    COLUMN1
18/02/2013 1.35
19/02/2013 1.36

I want to add all the 'B' values (COLUMN2) in dollars But i have some that are in euros Column3 in TABLE1 shows dolar value in that date (FK_TAB_RATES)

What would be the sql query?

2
  • 2
    What exactly do you want to do? Just add if COLUMN1 has a value of DOLAR and COLUMN0 has a value of B? Or are you trying to convert euro's or something? Commented Feb 12, 2013 at 23:30
  • 3
    This question is very difficult to understand. Commented Feb 12, 2013 at 23:31

3 Answers 3

2

My guess is your Tab_Rates table is the currency multiplier for that given day. If so, something like this might work:

SELECT SUM (T.Column2 *
    CASE 
        WHEN T.Column1 = 'Euro'
        THEN TR.Column1
        ELSE 1.0
    END) SummedAmt
FROM Table1 T
    JOIN Tab_Rates TR ON T.Column3 = TR.Column0
WHERE T.Column0 = 'B'

And here is a SQL Fiddle to demo it.

This produces 35.72. Doing it manually, 4.5 + 1.3 + 22(1.36) = 35.72

Good luck.

Sign up to request clarification or add additional context in comments.

Comments

1

Try something like this (untested)

select t1.col0,
    sum (case when t1.col1 = 'euro' then t1.col1 * rate.r
              else t1.col1 end) as s
from t1
inner join rate on rate.r = t1.col3
group by col0

Comments

0

try this:

SELECT SUM (T.Column2 *
    CASE 
        WHEN T.Column1 = 'Euro'
        THEN TR.Column1
        ELSE 1.0
    END) SummedAmt
FROM Table1 T
    JOIN Tab_Rates TR ON T.Column3 = TR.Column0
WHERE T.Column0 = 'B'

Comments

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.