如图所示,想取从第一次发货时间开始算3个月内的发货数或者金额select p.shp_name,sum(b.quantity) as '三期发货数',sum(b.quantity*b.prd_price) as '三期发货额' from dat_ckpf_dtl b left join ( select convert(date,(CONVERT(CHAR(8), (min(a.sys_locdate)), 112))) as '首次发货日',a.shp_id_to,a.id,c.shp_name,c.shp_fax ,dateadd(MONTH,6,convert(date,(CONVERT(CHAR(8), (min(a.sys_locdate)), 112)))) as '第二期',a.sys_locdate ,dateadd(MONTH,9,convert(date,(CONVERT(CHAR(8), (min(a.sys_locdate)), 112)))) as '第三期',c.shp_email from dat_ckpf a ,BAS_SHP c where c.shp_id = a.shp_id_to group by a.shp_id_to,a.id ,c.shp_name,c.shp_fax,a.sys_locdate,c.shp_email ) p on p.id = b.id where p.shp_email = '云南战区' and CONVERT(CHAR(8), p.sys_locdate, 112) >= p.第二期and CONVERT(CHAR(8), p.sys_locdate, 112) < p.第三期 group by p.shp_name