表 kc (商品出入库表)列字段如下
id , int(1,1)
wid, int '商品ID
num, float 商品数量
tprice, money 商品金额
irck ,tinyint (0,入库,1 出库) 默认0
ddate , datetime(入库/出库日期,)
wtype ,tinyint(商品类别)
表 SP 商品表
wid ,int 商品ID
wname ,varchar(50) 商品名称
wtype 商品类别
我想求得所有商品大类 的本月入库数量,本月入库金额,本月出库数量,本月出库金额, 上月末节余数量,节余金额,本月末数量,本月末金额
请问怎么写这个存储过程,用SQL语句也行,只要能实现
id , int(1,1)
wid, int '商品ID
num, float 商品数量
tprice, money 商品金额
irck ,tinyint (0,入库,1 出库) 默认0
ddate , datetime(入库/出库日期,)
wtype ,tinyint(商品类别)
表 SP 商品表
wid ,int 商品ID
wname ,varchar(50) 商品名称
wtype 商品类别
我想求得所有商品大类 的本月入库数量,本月入库金额,本月出库数量,本月出库金额, 上月末节余数量,节余金额,本月末数量,本月末金额
请问怎么写这个存储过程,用SQL语句也行,只要能实现
MS SQLSERVER数据库管理提供:一、SQL备份恢复(包括本地备份和远程备份)。 二、SQL与第三方数据格式(EXCEL,ACCESS,DBF,TXT,远程MS SQLSERVER)导入导出 三、SQL语句查询、查询结果报表打印、执行语句提交 四、SQL编程资料参考。CSDN下载:
数据库开发助手(DBDevTools V4.5)
http://down.csdn.net/kfgj/other/14512.html
wtype as 商品类别,
本月入库数量,
本月入库金额,
本月出库数量,
本月出库金额,
(上月底入库总量-上月底出库总量) as 上月末节余数量,
(上月底入库总额-上月底出库总额) as 上月末节余金额,
(上月底入库总量+本月入库数量-上月底出库总量-本月出库数量) as 本月末数量,
(上月底入库总额+本月入库金额-上月底出库总额-本月出库金额) as 本月末金额
from
(
select
wtype,
sum(case when irck=0 and datediff(month,ddate,getdate())=0 then num else 0 end) as 本月入库数量,
sum(case when irck=0 and datediff(month,ddate,getdate())=0 then tprice else 0 end) as 本月入库金额,
sum(case when irck=1 and datediff(month,ddate,getdate())=0 then num else 0 end) as 本月出库数量,
sum(case when irck=1 and datediff(month,ddate,getdate())=0 then tprice else 0 end) as 本月出库金额,
sum(case when irck=0 and datediff(month,ddate,getdate())>0 then num else 0 end) as 上月底入库总量,
sum(case when irck=0 and datediff(month,ddate,getdate())>0 then tprice else 0 end) as 上月底入库总额,
sum(case when irck=1 and datediff(month,ddate,getdate())>0 then num else 0 end) as 上月底出库总量,
sum(case when irck=1 and datediff(month,ddate,getdate())>0 then tprice else 0 end) as 上月底出库总额
from SP header inner join KC detail
on header.wid=detail.wid
group by wtype
)a
wtype as 商品类别,
本月入库数量,
本月入库金额,
本月出库数量,
本月出库金额,
(上月底入库总量-上月底出库总量) as 上月末节余数量,
(上月底入库总额-上月底出库总额) as 上月末节余金额,
(上月底入库总量+本月入库数量-上月底出库总量-本月出库数量) as 本月末数量,
(上月底入库总额+本月入库金额-上月底出库总额-本月出库金额) as 本月末金额
from
(
select
wtype,
sum(case when irck=0 and datediff(month,ddate,getdate())=0 then num else 0 end) as 本月入库数量,
sum(case when irck=0 and datediff(month,ddate,getdate())=0 then tprice else 0 end) as 本月入库金额,
sum(case when irck=1 and datediff(month,ddate,getdate())=0 then num else 0 end) as 本月出库数量,
sum(case when irck=1 and datediff(month,ddate,getdate())=0 then tprice else 0 end) as 本月出库金额,
sum(case when irck=0 and datediff(month,ddate,getdate())>0 then num else 0 end) as 上月底入库总量,
sum(case when irck=0 and datediff(month,ddate,getdate())>0 then tprice else 0 end) as 上月底入库总额,
sum(case when irck=1 and datediff(month,ddate,getdate())>0 then num else 0 end) as 上月底出库总量,
sum(case when irck=1 and datediff(month,ddate,getdate())>0 then tprice else 0 end) as 上月底出库总额
from SP header left join KC detail --这里
on header.wid=detail.wid
group by wtype
)a