0

Need your assistance on building a SQL query on the below requirement:

I have two tables as below:

TABLE A:

TASKNAME           HOSTNAME
---------------------------
12                 A1
34                 B
22                 C1
43                 D
65                 E1
93                 F

TABLE B:

HOST              NODE
----------------------
X                 ABC
X                 ABC
Y                 BV
B                 YTW
B                 IJK
D                 WWQ
D                 UWY
N                 J1
F                 ING
F                 UPI

I need to run the SQL query to compare these two TABLES and from table A it has to look for HOSTNAME value with HOST field in table B and the final outcome should be like below :

TASKNAME           HOSTNAME
---------------------------
12                 A1
34                 YTW
34                 IJK
22                 C1
43                 WWQ
43                 UWY
65                 E1
93                 ING
93                 UPI

Values B, D and F are common between those two tables.

1
  • What have you tried so far? A search on using SQL JOIN would give you a starting point! Commented Dec 1, 2017 at 14:53

1 Answer 1

3

You need a JOIN and NVL:

SELECT A.TASKNAME,
       NVL(B.NODE,A.HOSTNAME) AS HOSTNAME
FROM TableA A
LEFT JOIN TableB B
   ON A.HOSTNAME = B.HOST;
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Lamak. Was checking your profile, did you apply for the Spanish moderator job?
@JuanCarlosOropeza Hi. You mean spanish SO?, well, anyway, I haven't nominated myself for a mod in any site in StackExchange

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.