原始数据

计算后数据

关键sql:
sum(isnull(case when InoutFlag=1 then gios.FactReceiptQuan else null end, 0)-isnull(case when InoutFlag=0 then gios.FactIssueQuan else null end, 0)) over(partition by gios.MNumber,gios.MArea,gios.BatchNum order by gios.MNumber,gios.MArea,gios.BatchNum,gios.InOutMainId,gios.InOutSubId) as 结余数量