表  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语句也行,只要能实现

解决方案 »

  1.   

    MS SQLSERVER数据库开发提供:一、查看指定数据库对象 二、加密的存储过程、函数、触发器及视图都可以解密 三、自助提供表的查询、增、删、改的SQL语句 四、针对现有表记录生成初始数据脚本 五、针对表生成管理此表的存储过程。 六、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
      

  2.   

    --tryselect
    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
      

  3.   

    看到得早了点而已^^,上月底的总计的计算负担会比较大,最好是能像liangpei2008(我爱世界杯)所说的那样有一个月结表
      

  4.   

    --那你试试这个select
    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