现在我有两个表ManagerDetail 和 ManagerDetailHistory
ManagerDetail表
ManagerName     sales      date
ACA              300       2007-11-21
AL               200       2007-11-21
FAS              400       2007-11-21
TTT              100       2007-11-21ManagerDetailHistory
ManagerName     sales      date
ACA              100       2007-11-14
AL               100       2007-11-14
FAS              100       2007-11-14
ACA              10        2007-11-13现在我要的结果是11-21日与11-14日的结果差值
ManagerName     sales      
ACA              200
AL               100
FAS              300
TTT              100我自己写的语句用的是左连接 ManagerDetail 作为主表 然后ManagerDetailHistory的date字段作为时间的筛选字段,这样做的话TTT的信息就丢失了 所以希望SQL高手给个解决方案  花了几百分问sql的问题了 最后10分了 大家别嫌少  感谢感谢

解决方案 »

  1.   

    你的ManagerDetailHistory的date字段,不要放where里
    放在on條件里,這樣TTT就不丟了
      

  2.   

    select ManagerName,sales = (A.sales - ISNULL(B.sales,0)) from (SELECT * from ManagerDetail where date = '2007-11-21')A 
    left join (SELECT * from ManagerDetailHistory where date = '2007-11-14')B
    on A.ManagerName = B.ManagerName 
      

  3.   

    select A.ManagerName, A.sales-isnull(B.sales) as sales
    from ManagerDetail A
    left join ManagerDetailHistory B
    on A.ManagerName=B.ManagerName and B.date='2007-11-14'
    where A.date='2007-11-21'
      

  4.   

    select A.ManagerName,sales = (A.sales - ISNULL(B.sales,0)) from (SELECT * from ManagerDetail where date = '2007-11-21')A 
    left join (SELECT * from ManagerDetailHistory where date = '2007-11-14')B
    on A.ManagerName = B.ManagerName 
      

  5.   

    playwarcraft 非常感谢  也谢谢大家拉  :)