SQL-基于(从第一个表的行值到第二个表的列值)将一个表与另一个表连接起来

SQL-基于(从第一个表的行值到第二个表的列值)将一个表与另一个表连接起来,sql,sql-server,join,left-join,Sql,Sql Server,Join,Left Join,我有两张桌子,如下所示 第一张表: DealNum CurrencyValue CurrencyCode Date 110 100 AA 01/12/2011 111 200 AB 01/11/2011 112 120 AC

我有两张桌子,如下所示

第一张表:

   DealNum      CurrencyValue      CurrencyCode       Date

    110             100                 AA         01/12/2011  
    111             200                 AB         01/11/2011
    112             120                 AC         01/10/2011  
    113             20                  AA         01/11/2011
    110             103                 AD         01/12/2011  
    115             200                 AD         01/11/2011
    119             120                 AG         01/10/2011  
    130             20                  AK         01/11/2011
第二桌

   CurrencyCode     OCT       NOV      DEC     JAN   ..

      AA             0.91     0.88     0.9     0.94  
      AB             0.9      0.8      0.96    0.89
      AC             0.81     0.79     0.82    0.84  
      AD             0.4      0.41     0.42    0.39
      AE             0.9      0.92     0.91    0.94  
      AF             0.8      0.82     0.83    0.81
现在,我想将第二个表中的数据添加到新列中的第一个表中,条件如下

1.It has to do based on the CurrencyCode and month
2.If the deal is from DEC and currencyCode is AA , then it has to take the value 0.9,
  if the deal is from NOV and currencyCode is AA , then it has to take the value 0.88..
所以,结果应该是这样的

   DealNum      CurrencyValue      CurrencyCode       Date        NewColumn

    110             100                 AA         01/12/2011        0.9
    111             200                 AB         01/11/2011        0.8
    112             120                 AC         01/10/2011        0.81  
    113             20                  AA         01/11/2011        0.88
    110             103                 AD         01/12/2011        0.42
    115             200                 AD         01/11/2011        0.41
    119             120                 AG         01/10/2011         --
    130             20                  AK         01/11/2011         --
我不知道如何将第一个表中月份的行值和第二个表中月份的列值进行比较。请帮我做这个

提前谢谢

干杯,
Harish.

尽管第二张桌子设计得不好,但这应该是可行的

SELECT   DealNum      
       , CurrencyValue      
       , CurrencyCode       
       , Date
       , Deal = 
         CASE MONTH(t1.Date)
           WHEN 1 THEN t2.JAN
           WHEN 2 THEN t2.FEB
           WHEN 3 THEN t2.MAR
           -- .....
           WHEN 10 THEN t2.OCT
           WHEN 11 THEN t2.NOV
           WHEN 12 THEN t2.DEC
        END
FROM Table1 t1 
INNER JOIN Table2 t2 ON t1.CurrencyCode = t2.CurrencyCode 

谢谢Tim Schmelter。。这个很棒。。为我工作很好。但是,这里的问题是,第二个表中的列名(月份)每次都在不断变化,有没有什么动态的方法可以做到这一点,比如t1.date=t2.(XYZ月),然后是t2.Jan或t2.Feb???再次感谢您的帮助:))