分店 2008年1月 2008年2月份 ...    ...
s1 人数 销量           人数 销量 ...    ...
S2
求:按分店分组后的统计的数据,按月份显示查询的数据
 比如:(选择"起始日期 2008年3月" ,"结束日期 2008年5月",那么查询的结果将是 3月,4月,5月的数据)
 问题点:怎样写SQL日期条件?
 人数表结构: 表a, 字段 a1(人数), 字段a2(分店)  (意思:分店的销售人员)
 销量表结构: 表b, 字段 b1(分店), 字段b2(销售日期), b3(销售量) 

解决方案 »

  1.   

    假设日期存储格式20080521
    select a.a2,a.a1,substr(b.b2,1,6) as month,sum(b.b3)
    from a,b
    where a.a2=b.b1
    and substr(b.b2,1,6) between '200803' and '200805'
    group by a.a2,a.a1,substr(b.b2,1,6)
      

  2.   

    假设日期存储格式20080521 
    select a.a2,a.a1,substr(b.b2,1,6) as month,sum(b.b3) 
    from a,b 
    where a.a2=b.b1 
    and substr(b.b2,1,6) between '200803' and '200805' 
    group by a.a2,a.a1,substr(b.b2,1,6)  其中:between '200803' and '200805' 不能用固定的日期格式,而是用动态参数代替.
    怎样写动态条件查询条件?
      

  3.   


    如果是动态的
    select a.a2,a.a1,substr(b.b2,1,6) as month,sum(b.b3) 
    from a,b 
    where a.a2=b.b1 
    and substr(b.b2,1,6) between (('200803' and '200805' )or ('20080321' and '20080521' ))
    group by a.a2,a.a1,substr(b.b2,1,6) 
    试试,,没做测试
      

  4.   


    变动也应当是月份,比如换一个数between '200801'  and '200807',也不应该是日期, 
    '200801'   '200807' 换成变量就可以了