Bob
Bob

Reputation: 885

SQL multi if statement

I have two tables, Data and Rate. In the Data table, I have the following info:-

enter image description here

As for Rate table, I hv the following info:-

enter image description here

The final output should be in this format:-

enter image description here

The conditions:-

a. If both Local Currency and Document Current are the same, the ExRate must be equal to 1;

b. If they are not the same, then the ExRate will be based on the Local Currency in the Rate table.

How can I generate the above output?

Upvotes: 0

Views: 67

Answers (1)

TriV
TriV

Reputation: 5148

You could use LEFT JOIN and CASE

SELECT d.*, 
        CASE
            WHEN d.DocumentCurrency = r.LocalCurrency then 1
            ELSE r.ExRate
        END AS ExRate
FROM Data d
LEFT JOIN Rate r on d.DocumentCurrency = r.DocumentCurrency
                    AND d.LocalCurrency = r.LocalCurrency

Upvotes: 1

Related Questions