--查询上月入库数量 SELECT t3.FNUMBER AS '物料编码', t1.FDATE AS '入库时间', SUM( t2.FREALQTY ) AS '上月累计入库数量' FROM T_PRD_INSTOCK t1 LEFT JOIN T_PRD_INSTOCKENTRY t2 ON t1.FID = t2.FID LEFT JOIN T_BD_MATERIAL t3 ON t2.FMATERIALID = t3.FMATERIALID WHERE DATEDIFF(month,t1.FDATE,GETDATE())=1 GROUP BY t1.FDATE, t3.FNUMBER ORDER BY t1.FDATE DESC --查询本月入库数量 SELECT t3.FNUMBER AS '物料编码', t1.FDATE AS '入库时间', sum( t2.FREALQTY ) AS '本月累计入库数量' FROM T_PRD_INSTOCK t1 LEFT JOIN T_PRD_INSTOCKENTRY t2 ON t1.FID = t2.FID LEFT JOIN T_BD_MATERIAL t3 ON t2.FMATERIALID = t3.FMATERIALID WHERE DATEDIFF(month,t1.FDATE,GETDATE())=0 GROUP BY t1.FDATE, t3.FNUMBER ORDER BY t1.FDATE DESC