Search code examples
sqljoindatatableinner-join

SQL noob, how to join two tables and store data into a new_table?


I have two tables that i want to join(in terms of symbol and date(only the YEAR have to match!!)) and store in a new_table, is that possible?

expected result:

symbol       date 1             value 1          date 2             value 2

"A"          "2010-12-31"       "29.635193133"   "2010-01-05"       "22.324748927"

1.

symbol   date           value_1
"A"     "2010-12-31"    "29.635193133"
"A"     "2011-12-30"    "24.985693133"
"A"     "2012-12-31"    "29.284693133"
"AAL"   "2010-12-31"    "10.01"
"AAL"   "2011-12-30"    "5.07"

and 2.

symbol   date           value_2
"A"     "2010-01-05"    "22.324748927"
"A"     "2011-01-05"    "29.5135915594"
"AAL"   "2012-01-05"    "5.15"
"AAL"   "2015-01-05"    "54.369999"

Solution

  • There are probably more issues to resolve, but here is a solution:

    create table table3 as 
    select *
    from table1
    inner join table2 
    on symbol1 = symbol2
    and year(date1) = year(date2)
    

    Working example: http://sqlfiddle.com/#!9/2cdf4f/1