你用了group by ,但不見你用sum()之類的函數﹖因為你只選擇出其中的字段﹐所以存在重復記錄的可能性很大。不選擇出重復記錄﹐用distinctselect distinct sellreport_tt.dealdate as xiaoshouriqi,isnull(sell_inv,0) as xiaoshoufapiao,isnull(mobileM,0) as shoujixiaoshoue
from sellreport_tt left outer join sellreport_dt_all on (sellreport_tt.dealdate=sellreport_dt_all.dealdate)
where sellreport_tt.dealdate=sellreport_dt_all.dealdate and sellreport_tt.storeid='fy01' and sellreport_tt.dealdate='2004-07-01'

解决方案 »

  1.   

    select sellreport_tt.dealdate as xiaoshouriqi,
           isnull(sellreport_tt.sell_inv,0) as xiaoshoufapiao,
           isnull(sellreport_dt_all.mobileM,0) as shoujixiaoshoue
    from sellreport_tt left outer join sellreport_dt_all
      on (sellreport_tt.dealdate=sellreport_dt_all.dealdate)
    where sellreport_tt.storeid='fy01' and sellreport_tt.dealdate='2004-07-01'
    group by sellreport_tt.dealdate,isnull(sellreport_tt.sell_inv,0),isnull(sellreport_dt_all.mobileM,0)
      

  2.   

    主要是分组不对,应根据isnull(..)分组
      

  3.   

    同意 pbsql(风云) 的观点其实像这种没有聚合函数的,楼主主要是要去重复,用distinct个人感觉好一点,效果一样
      

  4.   

    select distinct
    sellreport_tt.dealdate as xiaoshouriqi,
    isnull(sell_inv,0) as xiaoshoufapiao,
    isnull(mobileM,0) as shoujixiaoshoue
    from sellreport_tt 
    left outer join sellreport_dt_all
    on (sellreport_tt.dealdate=sellreport_dt_all.dealdate)
    where sellreport_tt.dealdate=sellreport_dt_all.dealdate
    and sellreport_tt.storeid='fy01'
    and sellreport_tt.dealdate='2004-07-01'
      

  5.   

    例如说,表sellreport_dt_all中单独查询可以查询出来16 个数据,表sellreport_tt 可以出现5个数据,我的想法是想对各自表中的 16个数据求和,5个数据求和,然后根据相同日期返回
      

  6.   

    --既然你只愿意说这么多,那也就根据你这么多写这么多select 日期=a.日期,值a=a.值,值b=b.值
    from( --先对 sellreport_dt_all 分组求和
    select 日期,值=sum(值) --按日期分组求值
    from sellreport_dt_all
    group by 日期
    )a,( --再对 sellreport_tt 分组求和
    select 日期,值=sum(值) --按日期分组求值
    from sellreport_tt
    group by 日期
    )b where a.日期=b.日期 --再合并求和结果
      

  7.   

    我按照你说的,这样写对么/
    select distinct
    sellreport_tt.dealdate as xiaoshouriqi,
    isnull(sell_inv,0) as xiaoshoufapiao,
    isnull(mobileM,0) as shoujixiaoshoue
    from (select sellreport_tt.dealdate as xiaoshouriqi, sum(isnull(sell_inv,0)) as xiaoshoufapiao from sellreport_tt 
    where dealdate='2004-07-01' and storeid='fy01'
            group by dealdate,storeid)
        ,(select sellreport_tt.dealdate as xiaoshouriqi, sum(isnull(mobileM,0)) as shoujixiaoshoue from sellreport_dt_all
    where dealdate='2004-07-01' and storeid='fy01'
     group by dealdate,storeid) 
    where sellreport_tt.dealdate=sellreport_dt_all.dealdate
    为什么出错呢?多谢指点
      

  8.   

    select distinct
    a.dealdate as xiaoshouriqi,
    isnull(sell_inv,0) as xiaoshoufapiao,
    isnull(mobileM,0) as shoujixiaoshoue
    from (
    select dealdate as xiaoshouriqi,
    sum(isnull(sell_inv,0)) as xiaoshoufapiao
    from sellreport_tt 
    where dealdate='2004-07-01' and storeid='fy01'
    group by dealdate,storeid
    )a --少了别名
    ,(
    -- select sellreport_tt.dealdate as xiaoshouriqi,--写错了字段名的引用
    select dealdate as xiaoshouriqi,
    sum(isnull(mobileM,0)) as shoujixiaoshoue
    from sellreport_dt_all
    where dealdate='2004-07-01' and storeid='fy01'
    group by dealdate,storeid
    ) b --少写了别名
    where a.dealdate=b.dealdate
      

  9.   

    select distinct
    a.dealdate as xiaoshouriqi,
    isnull(sell_inv,0) as xiaoshoufapiao,
    isnull(mobileM,0) as shoujixiaoshoue
    from (
    select dealdate as xiaoshouriqi,
    sum(isnull(sell_inv,0)) as xiaoshoufapiao
    from sellreport_tt 
    where dealdate='2004-07-01' and storeid='fy01'
    group by dealdate--,storeid--只按日期关联,应该不要再按storeid分组,否则可能导致重复
    )a --少了别名
    ,(
    -- select sellreport_tt.dealdate as xiaoshouriqi,--写错了字段名的引用
    select dealdate as xiaoshouriqi,
    sum(isnull(mobileM,0)) as shoujixiaoshoue
    from sellreport_dt_all
    where dealdate='2004-07-01' and storeid='fy01'
    group by dealdate--,storeid--只按日期关联,应该不要再按storeid分组,否则可能导致重复
    ) b --少写了别名
    where a.dealdate=b.dealdate
      

  10.   

    select distinct
    a.dealdate as xiaoshouriqi,
    isnull(sell_inv,0) as xiaoshoufapiao,
    isnull(mobileM,0) as shoujixiaoshoue
    from (
    select dealdate as xiaoshouriqi,
    sum(isnull(sell_inv,0)) as xiaoshoufapiao
    from sellreport_tt 
    where dealdate='2004-07-01' and storeid='fy01'
    group by dealdate,storeid
    )a --少了别名
    ,(
    -- select sellreport_tt.dealdate as xiaoshouriqi,--写错了字段名的引用
    select dealdate as xiaoshouriqi,
    sum(isnull(mobileM,0)) as shoujixiaoshoue
    from sellreport_dt_all
    where dealdate='2004-07-01' and storeid='fy01'
    group by dealdate,storeid
    ) b --少写了别名
    where a.dealdate=b.dealdate and a.storeid=b.storeid --如果按 storeid 分组是必须的
      

  11.   

    十分感谢大家,特别是5星大侠 zjcxc(邹建) 和 happyxyzw