如ta
    PRCDate       Amount    ReportNo   Cru
   03/22/2009       20         a01     USD
   05/05/2010       30         a02     HKD
   08/22/2010       40         a03     RMB
   08/22/2010       40         a03     USDtb
  years     Month     Cru    Rates
   2009       03      USD    6.5674
   2010       05      HKD    0.8676
   2010       08      USD    6.8992分割ta中PRCDate字段做条件(ta.PRCDate.mm=tb.month and ta.prcdate.yyyy=tb.years  and  ta.cru=tb.cru)
得到:
   PRCDate       Amount    ReportNo    Cru     Rates
   03/22/2009       20         a01     USD     6.5674
   05/05/2010       30         a02     HKD     0.8676
   08/22/2010       40         a03     RMB     1
   08/22/2010       40         a03     USD     6.8992

解决方案 »

  1.   

    ta中PRCDate 的日期格式为:mm/dd/yyyy
      

  2.   


    datepart(mm,ta.PRCDate)=convert(int,tb.month)
      

  3.   

    month(ta.PRCDate)=tb.month and year(ta.prcdate)=tb.years
      

  4.   

    当然可以。。ta.PRCDate.mm=tb.month and ta.prcdate.yyyy=tb.years and ta.cru=tb.cru更改为year(ta.PRCDate)=tb.years AND month(ta.PRCDate)=cast(tb.Month as int) and ta.cru=tb.cru
      

  5.   

    可以。where DATENAME(month,ta.PRCDate) = tb.[month]
     and DATENAME(year,ta.prcdate) = tb.years 
     and ta.cru=tb.cru
      

  6.   


    if OBJECT_ID('ta') is not null
    drop table ta
    go
    create table ta (PRCDate date,Amount int,ReportNo varchar(3),Cru varchar(3))
    insert into ta
    select   '03/22/2009', 20, 'a01', 'USD' union all
    select   '05/05/2010', 30, 'a02', 'HKD' union all
    select   '08/22/2010', 40, 'a03', 'RMB' union all
    select   '08/22/2010', 40, 'a03', 'USD'
    if OBJECT_ID('tb') is not null
    drop table tb
    go
    create table tb (years int,Month varchar(2),Cru varchar(3),Rates numeric(10,4))
    insert into tb
    select  '2009', '03', 'USD', 6.5674 union all
    select  '2010', '05', 'HKD', 0.8676 union all
    select  '2010', '08', 'USD', 6.8992
      
      select a.PRCDate,a.Amount,a.ReportNo,isnull(b.Rates,1) as Rates
      from ta a left join tb b on DATEPART(YEAR,a.PRCDate)=b.years 
           and DATEPART(MONTH,a.PRCDate)=cast(b.Month as int)
           and a.Cru=b.Cru
      
      PRCDate Amount ReportNo Rates
    2009-03-22 20 a01 6.5674
    2010-05-05 30 a02 0.8676
    2010-08-22 40 a03 1.0000
    2010-08-22 40 a03 6.8992
      

  7.   


    -trydeclare @ta table (prcdate datetime,amount int,reportno nvarchar(10),cru nvarchar(10))
    insert into @ta select '03/22/2009',20,'a01','usd'
         union all  select '05/05/2010',30,'a02','hkd'
         union all  select '08/22/2010',40,'a03','rmb'
         union all  select '08/22/2010',40,'a03','usd'
    declare @tb table(years int,month int,cru nvarchar(10),rates decimal(10,5))
    insert into @tb select 2009,03,'usd',6.5674
          union all select 2010,05,'hkd',0.8676
          union all select 2010,08,'usd',6.8992
    select a.*,b.rates from @ta a 
       join @tb b on year(a.prcdate)=b.years and month(a.prcdate)=b.month
    /*prcdate                 amount      reportno   cru        rates
    ----------------------- ----------- ---------- ---------- ---------------------------------------
    2009-03-22 00:00:00.000 20          a01        usd        6.56740
    2010-05-05 00:00:00.000 30          a02        hkd        0.86760
    2010-08-22 00:00:00.000 40          a03        rmb        6.89920
    2010-08-22 00:00:00.000 40          a03        usd        6.89920(4 行受影响)
    */