回92楼之跨年跨月查询 sheets("商品进出库期初期末").range("a1:d25").name="dataarea" sqltext="select * from (select 年,月,商品编号,sum(s结存数量+s销售数量-s进货数量) as 上月结存数,sum(s进货数量) as 月进货数量,sum(s销售数量) as 月销售数量,sum(s结存数量) as 本月结存数 from (select year(日期) as 年,format(month(日期),'00') AS 月,商品编号,sum(进货数量) as s进货数量,sum(销售数量) as s销售数量,0 as s结存数量 from dataarea group by year(日期),month(日期),商品编号 union all select 年,月,商品编号,0,0,sum(进货数量)-sum(销售数量) from dataarea,(select distinct year(日期) as 年 from dataarea) as kjnd,(select distinct format(month(日期),'00') as 月 from dataarea) as kjqj where year(日期)&format(month(日期),'00')<=年&月 group by 年,月,商品编号) group by 年,月,商品编号) where 月进货数量<>0 or 月销售数量<>0" |