I have the following two tables:
ExchangeRate (T1):
x
CurrencyCode Rate
CHF 6.9660
EUR 7.4445
GBP 8.2569
NOK 0.6686
Customer (T2):
CustomerNo Name Address City CurrencyCode
Test001 Test 1/2 Test TA NOK
Test002 Test 1/2 Test EU EUR
I want a result as below:
CurrencyCode CustomerNo City
CHF NULL NULL
EUR NULL NULL
GBP NULL NULL
NOK Test001 TA
CHF NULL NULL
EUR Test002 EU
GBP NULL NULL
NOK NULL NULL
I have tried this query
Select Er.CurrencyCode,Ca.CurrencyCode
From
ExchangeRate Er
Left Outer Join Customer As Ca With(NoLock) On Ca.CurrencyCode=Er.CurrencyCode
where CA.CustomerCode='Test001'
but, I got this result
CurrencyCode CustomerNo City
NOK Test001 TA
I need the result like Cross Join
. Kindly help me to resolve this issue.
Advertisement
Answer
Use a cross join
with a case
expression to determine whether to display the results or not.
select Er.CurrencyCode
, case when Ca.CurrencyCode = Er.CurrencyCode then Ca.CustomerNo end CustomerNo
, case when Ca.CurrencyCode = Er.CurrencyCode then Ca.City end City
from ExchangeRate Er
cross join Customer Ca
where CA.CustomerCode = 'Test001'
Note: Don’t use nolock
unless you absolutely must and are clear about the possible consequences.