0

i've been trying to build this query. hoping someone can help. I have 2 tables.

1 table contains

Code | name | Value | Period  
1      name1   1      2010  
2      name2   2      2010  

table 2 contains

code | name |  
 1    name1  
 2    name2  
 3    name3  
 4    name4  

what i want to be displayed is

1 name1 1  
2 namw2 2   
3 name3 0  
4 name4 0  

In some instances table 1 may have a value for all name variables in table 2 but where there are only 1,2,3 names i want it to display the other one but with a value of 0 or blank.

SMA
  • 36,381
  • 8
  • 49
  • 73
  • Could you rephrase your question and check the fieldnames? Is it really `1 name 1` or it is just `name 1` and you added the field id? – RST Nov 04 '14 at 10:59
  • What all have you tried so far? – Jason Nov 04 '14 at 11:02
  • possible duplicate of [SQL Join / Union](http://stackoverflow.com/questions/26735382/sql-join-union) – AHiggins Nov 04 '14 at 14:15
  • This is the same question you asked [here](http://stackoverflow.com/questions/26735382/sql-join-union), but with more generic names for your tables and columns. Next time, you should edit the original question with new information, rather than posting a new question. – AHiggins Nov 04 '14 at 14:16

1 Answers1

1

Try this:

select 
    T2.*, 
    isnull(T1.code, 0) as code -- or value
from 
    table2 T2 
    left outer join table1 T1 on T1.name = T2.name

You can replace isnull(T1.code, 0) as code with isnull(T1.value, 0) as value. I'm not sure what you're after ...

SmartDev
  • 2,802
  • 1
  • 17
  • 22