进销存统计表

页面导航:首页 > 数据库 > MsSql > 进销存统计表

进销存统计表

来源: 作者: 时间:2016-01-20 09:48 【

-- ============================================= -- Author: -- Create date: -- @SQJ:开始期间 @EQJ:结束期间 @PH:品号 @C
-- =============================================   
-- Author: <未知-偶然从QQ群文件位置获取>   
-- Create date: <2011-06-23>  
-- @SQJ:开始期间 @EQJ:结束期间 @PH:品号 @CK:仓库 @F:数量为0不显示,1:不显示 
-- Description: <销售日报分析>   
-- ============================================= 
ALTER PROC JXC(@SQJ VARCHAR(6),@EQJ VARCHAR(6),@PH VARCHAR(200),@CK VARCHAR(200),@FL VARCHAR(200),@F INT)
AS
DECLARE @SQL VARCHAR(2000),@SQJA VARCHAR(8),@EQJA VARCHAR(8)
--,@SQJ VARCHAR(6),@EQJ VARCHAR(6),@PH VARCHAR(200),@CK VARCHAR(200),@FL VARCHAR(200),@F INT
--SET @SQJ='201401'
--SET @EQJ='201412'
--SET @PH=''
--SET @CK=''
--SET @FL=''
--SET @F=1

SET @[email protected]+'01'
SET @[email protected]+'31'

IF object_id('tempdb..##JXC') is not null    
  drop table ##JXC
IF object_id('tempdb..##JXCA') is not null    
  drop table ##JXCA
IF object_id('tempdb..##JXCB') is not null    
  drop table ##JXCB

CREATE TABLE ##JXC(FL VARCHAR(10),FLA VARCHAR(20),LX VARCHAR(10),PH VARCHAR(20),CK VARCHAR(10),QTY NUMERIC(16,6),JE NUMERIC(16,4))
--LA005:进出库 LA011:单据交易库存数量 LA013:单据金额
--MA004:存货会计科目 MA003:品号类别名称 LA001:品号 LA009:仓库 进出库数量:SUM(LA011*LA005) 进出库金额 SUM(LA013*LA005)
--LA014:按单据性质的交易类别判断码(1.入库、2.销货、3.领用、4.调拨、5.调整)
SET @SQL='INSERT INTO ##JXC SELECT MA004,MA003,''期初'',LA001,LA009,SUM(LA011*LA005),SUM(LA013*LA005) FROM INVLA LEFT JOIN INVMB ON LA001=MB001
LEFT JOIN (select * from INVMA WHERE MA001=2) INVMA ON MB005=MA002 LEFT JOIN CMSMC ON LA009=MC001 WHERE MC004=1 AND LA004<'''[email protected]+''''

IF @PH<>''
   BEGIN
      SET @[email protected]+' AND '[email protected]
   END
IF @CK<>''
   BEGIN
      SET @[email protected]+' AND '[email protected]      
   END
IF @FL<>''
   BEGIN
      SET @[email protected]+' AND '[email protected]    
   END    
SET @[email protected]+' GROUP BY MA004,MA003,LA001,LA009'
EXEC(@SQL)

SET @SQL='SELECT LA001,LA009,MA004,MA003,
          ISNULL(SUM(CASE WHEN LA014=1 AND LA005=1 THEN LA011 ELSE 0 END),0) A,
          ISNULL(SUM(CASE WHEN LA014=1 AND LA005=1 THEN LA013 ELSE 0 END),0) AJE,
          ISNULL(SUM(CASE WHEN LA014=1 AND LA005=-1 THEN LA011 ELSE 0 END),0) B,
          ISNULL(SUM(CASE WHEN LA014=1 AND LA005=-1 THEN LA013 ELSE 0 END),0) BJE,

          ISNULL(SUM(CASE WHEN LA014=2 AND LA005=1 THEN LA011 ELSE 0 END),0) D,
          ISNULL(SUM(CASE WHEN LA014=2 AND LA005=1 THEN LA013 ELSE 0 END),0) DJE,
          ISNULL(SUM(CASE WHEN LA014=2 AND LA005=-1 THEN LA011 ELSE 0 END),0) C,
          ISNULL(SUM(CASE WHEN LA014=2 AND LA005=-1 THEN LA013 ELSE 0 END),0) CJE,

          ISNULL(SUM(CASE WHEN LA014=3 AND LA005=1 THEN LA011 ELSE 0 END),0) F,
          ISNULL(SUM(CASE WHEN LA014=3 AND LA005=1 THEN LA013 ELSE 0 END),0) FJE,
          ISNULL(SUM(CASE WHEN LA014=3 AND LA005=-1 THEN LA011 ELSE 0 END),0) E,
          ISNULL(SUM(CASE WHEN LA014=3 AND LA005=-1 THEN LA013 ELSE 0 END),0) EJE,

          ISNULL(SUM(CASE WHEN LA014=4 AND LA005=1 THEN LA011 ELSE 0 END),0) G,
          ISNULL(SUM(CASE WHEN LA014=4 AND LA005=1 THEN LA013 ELSE 0 END),0) GJE,
          ISNULL(SUM(CASE WHEN LA014=4 AND LA005=-1 THEN LA011 ELSE 0 END),0) H,
          ISNULL(SUM(CASE WHEN LA014=4 AND LA005=-1 THEN LA013 ELSE 0 END),0) HJE,

          ISNULL(SUM(CASE WHEN LA014=5 AND LA005=1 THEN LA011 ELSE 0 END),0) I,
          ISNULL(SUM(CASE WHEN LA014=5 AND LA005=1 THEN LA013 ELSE 0 END),0) IJE,
          ISNULL(SUM(CASE WHEN LA014=5 AND LA005=-1 THEN LA011 ELSE 0 END),0) J,
          ISNULL(SUM(CASE WHEN LA014=5 AND LA005=-1 THEN LA013 ELSE 0 END),0) JJE

          INTO ##JXCA FROM INVLA LEFT JOIN INVMB ON LA001=MB001 LEFT JOIN (select * from INVMA WHERE MA001=2) INVMA ON MB005=MA002 WHERE LA004>='''[email protected]+''' AND LA004<='''[email protected]+''''
IF @PH<>''
   BEGIN
      SET @[email protected]+' AND '[email protected]
   END
IF @CK<>''
   BEGIN
      SET @[email protected]+' AND '[email protected]      
   END
IF @FL<>''
   BEGIN
      SET @[email protected]+' AND '[email protected]    
   END    

SET @[email protected]+' GROUP BY LA001,LA009,MA004,MA003 '
PRINT @SQL
EXEC(@SQL)

--插入期间数据至总表(考虑品号在期初与期间问题合并至同一表再作分列)
INSERT INTO ##JXC SELECT MA004,MA003,'入库',LA001,LA009,A,AJE FROM ##JXCA
INSERT INTO ##JXC SELECT MA004,MA003,'出库',LA001,LA009,B,BJE FROM ##JXCA
INSERT INTO ##JXC SELECT MA004,MA003,'销货',LA001,LA009,C,CJE FROM ##JXCA
INSERT INTO ##JXC SELECT MA004,MA003,'销退',LA001,LA009,D,DJE FROM ##JXCA
INSERT INTO ##JXC SELECT MA004,MA003,'领料',LA001,LA009,E,EJE FROM ##JXCA
INSERT INTO ##JXC SELECT MA004,MA003,'退料',LA001,LA009,F,FJE FROM ##JXCA
INSERT INTO ##JXC SELECT MA004,MA003,'调拨入',LA001,LA009,G,GJE FROM ##JXCA
INSERT INTO ##JXC SELECT MA004,MA003,'调拨出',LA001,LA009,H,HJE FROM ##JXCA
INSERT INTO ##JXC SELECT MA004,MA003,'调整入',LA001,LA009,I,IJE FROM ##JXCA
INSERT INTO ##JXC SELECT MA004,MA003,'调整出',LA001,LA009,J,JJE FROM ##JXCA

 SELECT FL,FLA,RTRIM(PH) 品号,CK 仓库,
 ISNULL(SUM(CASE WHEN LX='期初' THEN QTY ELSE 0 END),0) 月初数量,ISNULL(SUM(CASE WHEN LX='期初' THEN JE ELSE 0 END),0) 月初金额,
 ISNULL(SUM(CASE WHEN LX='入库' THEN QTY ELSE 0 END),0) 入库数量,ISNULL(SUM(CASE WHEN LX='入库' THEN JE ELSE 0 END),0) 入库金额,
 ISNULL(SUM(CASE WHEN LX='出库' THEN QTY ELSE 0 END),0) 出库数量,ISNULL(SUM(CASE WHEN LX='出库' THEN JE ELSE 0 END),0) 出库金额,
 ISNULL(SUM(CASE WHEN LX='销货' THEN QTY ELSE 0 END),0) 销货数量,ISNULL(SUM(CASE WHEN LX='销货' THEN JE ELSE 0 END),0) 销货金额,
 ISNULL(SUM(CASE WHEN LX='销退' THEN QTY ELSE 0 END),0) 销退数量,ISNULL(SUM(CASE WHEN LX='销退' THEN JE ELSE 0 END),0) 销退金额,
 ISNULL(SUM(CASE WHEN LX='领料' THEN QTY ELSE 0 END),0) 领料数量,ISNULL(SUM(CASE WHEN LX='领料' THEN JE ELSE 0 END),0) 领料金额,
 ISNULL(SUM(CASE WHEN LX='退料' THEN QTY ELSE 0 END),0) 退料数量,ISNULL(SUM(CASE WHEN LX='退料' THEN JE ELSE 0 END),0) 退料金额,
 ISNULL(SUM(CASE WHEN LX='调拨入' THEN QTY ELSE 0 END),0) 调拨入数量,ISNULL(SUM(CASE WHEN LX='调拨入' THEN JE ELSE 0 END),0) 调拨入金额,
 ISNULL(SUM(CASE WHEN LX='调拨出' THEN QTY ELSE 0 END),0) 调拨出数量,ISNULL(SUM(CASE WHEN LX='调拨出' THEN JE ELSE 0 END),0) 调拨出金额,
 ISNULL(SUM(CASE WHEN LX='调整入' THEN QTY ELSE 0 END),0) 调整入数量,ISNULL(SUM(CASE WHEN LX='调整入' THEN JE ELSE 0 END),0) 调整入金额,
 ISNULL(SUM(CASE WHEN LX='调整出' THEN QTY ELSE 0 END),0) 调整出数量,ISNULL(SUM(CASE WHEN LX='调整出' THEN JE ELSE 0 END),0) 调整出金额 
 INTO ##JXCB FROM ##JXC LEFT JOIN CMSMC ON CK=MC001 WHERE MC004=1 GROUP BY FL,FLA,PH,CK

SET @SQL='SELECT RTRIM(FL)+''-''+RTRIM(FLA) 分类,品号,RTRIM(MB002) 品名,RTRIM(MB003) 规格,MB004 单位,仓库,RTRIM(MC002) 仓库名称,月初数量,月初金额,月初数量+入库数量-出库数量
     +销退数量-销货数量+退料数量-领料数量+调拨入数量-调拨出数量+调整入数量-调整出数量 期未数量,月初金额+入库金额-出库金额
     +销退金额-销货金额+退料金额-领料金额+调拨入金额-调拨出金额+调整入金额-调整出金额 期未金额,入库数量,入库金额,出库数量,出库金额,
     销货数量,销货金额,销退数量,销退金额,领料数量,领料金额,退料数量,退料金额,调拨入数量,调拨入金额,调拨出数量,调拨出金额,
     调整入数量,调整入金额,调整出数量,调整出金额,入库数量-出库数量 入库净量,入库金额-出库金额 入库净额,销货数量-销退数量 销货净量,
     销货金额-销退金额 销货净额,领料数量-退料数量 领料净量,领料金额-退料金额 领料净额,调拨入数量-调拨出数量 调拨净量,调拨入金额-
     调拨出金额 调拨净额,调整入数量-调整出数量 调整净量,调整入金额-调整出金额 调整净额 FROM ##JXCB LEFT JOIN INVMB ON 
     品号=MB001 LEFT JOIN CMSMC ON 仓库=MC001 '
IF @F=1
BEGIN
   SET @[email protected]+' WHERE 月初数量+入库数量+出库数量+销退数量+销货数量+退料数量+领料数量+调拨入数量+调拨出数量+调整入数量+调整出数量+
                         月初金额+入库金额+出库金额+销退金额+销货金额+退料金额+领料金额+调拨入金额+调拨出金额+调整入金额+调整出金额<>0 '
END

SET @[email protected]+' ORDER BY FL,品号'
EXEC(@SQL)

DROP TABLE ##JXC
DROP TABLE ##JXCA
DROP TABLE ##JXCB

这里写图片描述
这里写图片描述

Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<