【问题标题】:SQL - Joining one table with another based on ( Row value from first table to Column value of second table)SQL - 根据(第一个表的行值到第二个表的列值)将一个表与另一个表连接起来
【发布时间】:2012-04-20 10:07:06
【问题描述】:

我有两张表,如下所示

第一个表:

   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         --

我不知道如何将第一个表中的月份行值与第二个表中月份的列值进行比较。请帮我解决这个问题..

提前致谢。

干杯, 哈里什。

【问题讨论】:

    标签: sql sql-server join left-join


    【解决方案1】:

    这应该可以,虽然你的第二张桌子设计得不好。

    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。二月???再次感谢您的帮助:))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    相关资源
    最近更新 更多