select a.销售日期,a.分店编号,a.销售额,isnull(b.销售额,0) from 表 a full join 表 b on a.销售日期=dateadd(month,-1,b.销售日期)

解决方案 »

  1.   

    你的销售日期是按月连续的吗??
    select a.销售日期,a.分店编号,a.销售额,isnull(b.销售额,0) 上月同期销售额 from 表 a full join 表 b on a.销售日期=dateadd(month,-1,b.销售日期)
      

  2.   

    用datediff,或者格式化a,b的日期格式
      

  3.   


    create table ta(销售日期 datetime,分店编码 char(10),销售额 numeric(11,2))
    insert into ta 
    select '2002-10-1','       001',         50 union 
    select '2002-10-1','       002',         70 union 
    select '2002-11-1','       001',         90 union 
    select '2002-11-1','       002',         80select a.*,isnull(b.销售额,0) from ta a left join 
    (select 销售日期 = dateadd(month,1,销售日期),
           分店编码,
           销售额
     from ta) as b
    on a.分店编码 = b.分店编码 and a.销售日期 = b.销售日期
      

  4.   

    這樣應該可以!select a.*,b.[sales value]
    from(select [date1],[branch_no],[sales value]
         from [table]) a left join
    (select [date1],[branch_no],[sales value]
     from [table]) b on a.[date1]=dateadd("m",1,b.[date1]) and 
     a.[branch_no]=b.[branch_no]
      

  5.   

    to pendali:
    select a.销售日期,a.分店编号,a.销售额,isnull(b.销售额,0) 
    from 表 a full join 表 b 
    on a.销售日期=dateadd(month,-1,b.销售日期)
    其中,条件on a.销售日期=dateadd(month,-1,b.销售日期)应该为
    on a.销售日期=dateadd(month,1,b.销售日期)
    另外full连接也会有问题。
      

  6.   

    select t.销售日期,t.分店编号,t.销售额,t1.销售额 as 上月同期销售额
    from T
           left join T t1 
                     on T.分店编号 = t1.分店编号
                        and dateadd(month,1,T.销售日期) = t1.销售日期 
      

  7.   

    真的很感谢谢大家给我的帮助,我已经调试成功了,感谢(chiff,大力,playyuer(小干部儿))
    我要给你们加分