同一张表可以用自关联,日期关联一周前的一天,然后主表控制参数周的开始时间和结束时间就可以,大体思路:
select a.月份,a.分类,a.名称,a.日期,a.收入,a.数量,a.单价,
b.月份,b.分类,b.名称,b.日期,b.收入,b.数量,b.单价
from 客源房费明细 as a
left join 客源房费明细 as b
on date_add(a.日期,interval -7 day)=b.日期
and a.分类=b.分类
and a.名称=b.名称
where 1=1
${if(len(dateinweek(time,1))==0,""," and a.日期>='"+dateinweek(time,1)+"'")}
${if(len(dateinweek(time,-1))==0,""," and a.日期<='"+dateinweek(time,-1)+"'")}
${if(len(分类)==0,""," and a.分类 in ('"+分类+"')")}
${if(len(名称)==0,""," and a.名称 in ('"+名称+"')")}