优化数据库结构和sql语句

应用实例记录

/*一、先求库存数量变化的三种情况*/

select wzid,storageid as st1,storageid2 as st2,

    sum(case when inoutflag=2 then amount else 0 end) as  入库,

    sum(case when inoutflag=4 then amount else 0 end) as  出库,

    sum(case when inoutflag=6 then amount else 0 end) as  调拨出库

from viewinoutall where 1=1 group by wzid,storageid,storageid2 order by wzid,storageid,storageid2

/*二、后求变化的一种情况*/

select * from (

select top 100 percent wzid,storageid2 as st,storageid as st2,

    sum(case when inoutflag=6 then amount else 0 end) as  调拨入库

from viewinoutall where 1=1 group by wzid,storageid2,storageid order by wzid,storageid2,storageid

) as aaa where st is not null

/*三、合并(略)*/

附图

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容