上面是数据库的结构
这个是我生成的表我想统计表中所有从beginmonth到endmonth的ordernum的和,比如上图第一行beginmonth和endmonth分别是2010-2-1到2010-3-1,那么ordernum的和就是47,这个语句该怎么写,我本来以为加个SUM就可以了,但是发现不行SELECT 
a.ordermonth as beginmonth,
b.ordermonth as endmonth
FROM 
monthlyorders a
INNER JOIN 
monthlyorders b
ON
b.ordermonth > a.ordermonth;

解决方案 »

  1.   

    SELECT 
        a.ordermonth as beginmonth,
        b.ordermonth as endmonth,
    (select sum(ordernum) from monthlyorders where ordermonth between a.ordermonth and b.ordermonth)
    FROM 
        monthlyorders a
    INNER JOIN 
        monthlyorders b
    ON
        b.ordermonth > a.ordermonth;
      

  2.   

    select  A.ordermonth,(select sum(ordernum) from monthlyorders B where A.ordermonth>=B.ordermonth)
    from monthlyorders A