select a.用户名,a.品种代码,b.日期,b.实销数 from table1 a left join table2 b on a.品种代码=b.品种代码 where 日期=@外部变量 日期

解决方案 »

  1.   

    select 表1.用户名,表1.品种代码,temp1.日期,temp1.实销数
    from 表1
        left join
        (select 品种代码,日期,sum(实销数) as 实销数 from 表2 group by 品种代码,日期) as temp1
             on 表1.品种代码=temp1.品种代码
      

  2.   

    补一下:
    select 表1.用户名,表1.品种代码,temp1.日期,temp1.实销数
    from 表1
        join
        (select 品种代码,日期,sum(实销数) as 实销数 from 表2 group by 品种代码,日期) as temp1
             on 表1.品种代码=temp1.品种代码
    where temp1.日期=@日期
      

  3.   

    select 表1.用户名,表1.品种代码,表2.日期,表2.实销数
    from 表1,表2
    where 表1.品种代码=表2.品种代码 and 表2.日期=@日期
    group by 表1.品种代码,日期