Prashant1987
Prashant1987

Reputation: 15

Adding column value dynamically in select statement SQL Query

I have 2 SQL tables like below

Table1

ServerName  Downloaded  Failed RebootRequried
server1     3            2      Yes
Server2     4            1      NO

Table2

ServerName    Administartor
server1       John
server3       Alex

I want to join these 2 tables so that I can extract the administrator name out of Table 2.

If the serverName of Table 1 is not matching with serverName of Table2 , then I want to retain all the columns ServerName,Downloaded,Failed, RebootRequired, Administrator(Which will be null).

If ServerName matches then all the columns should be retained including administrator name from Table2?

How can we do it with select statement in sql? I am pretty new to it ,and not sure how to use the conditional statement in sql

Upvotes: 1

Views: 489

Answers (1)

Brave Soul
Brave Soul

Reputation: 3620

you need to use left join. join tables1 and table2 on servername.

for more insight go through tutorials about joins

SELECT 
ServerName,
Downloaded,
Failed, 
RebootRequired, 
Administrator
FROM 
Table1 t1 
left join Table2 t2 
on 
t1.ServerName=t2.ServerName

Upvotes: 3

Related Questions