SP笔记:交叉实现七行并成一行

set  ANSI_NULLS  ON
set  QUOTED_IDENTIFIER  ON
go



--  =============================================
--
 Author:        CHENQP
--
 Create date: 9:37 2006-10-30
--
 Description:    获取挑选库存货位信息查询、用于领导决策系统
--
 交叉实现七行并成一行
--
 =============================================
ALTER   PROCEDURE   [ dbo ] . [ usp_TBManagerQuery_GetResortedTLWarehouse ]  
    
@strWhere    varchar ( 1500 =   ''    --  查询条件 (注意: 不要加 where)
AS
BEGIN
    
SET  NOCOUNT  ON ;
    
declare   @strSQL     varchar ( 5000 )        --  主语句  
     Create   Table  #TMPTABLE_1(IDD  int   identity ( 1 , 1 ),
    V_GoodsPlace 
VARCHAR ( 50 ) ,I_Package  INT  ,
    N_Weight 
DECIMAL ( 18 , 3 ),V_OutGrade  VARCHAR ( 50 ),
    V_Customer 
VARCHAR ( 50 ),V_PurchaseType  VARCHAR ( 50 ),V_AreaName  VARCHAR ( 50 ))
  
if   @strWhere   != ''   
    
set   @strSQL   =   '
    INSERT INTO #TMPTABLE_1
        SELECT V_GoodsPlace,
            COUNT(*) as I_Package,
            SUM(N_Weight) as N_Weight,
            V_OutGrade,
            V_Customer,
            V_PurchaseType,
            dbo.FUN_GetProductionAreaName(I_OutOriginID) AS V_AreaName            
        FROM V_NPick_StorageStream where 
'
    
+   @strWhere   +  
    
' GROUP BY V_GoodsPlace,V_OutGrade,V_Customer,V_PurchaseType,I_OutOriginID
        ORDER BY V_GoodsPlace
'
else
    
begin
  
set   @strSQL   =   '
    INSERT INTO #TMPTABLE_1
        SELECT V_GoodsPlace,
            COUNT(*) as I_Package,
            SUM(N_Weight) as N_Weight,
            V_OutGrade,
            V_Customer,
            V_PurchaseType,
            dbo.FUN_GetProductionAreaName(I_OutOriginID) AS V_AreaName            
        FROM V_NPick_StorageStream
        GROUP BY V_GoodsPlace,V_OutGrade,V_Customer,V_PurchaseType,I_OutOriginID
        ORDER BY V_GoodsPlace
'
    
end
    
    
execute ( @strSQL )    
--  货位小计信息
     SELECT   COUNT ( * as  GoodsPlaceCnt, SUM (I_Package)  as  PackageSum, SUM (N_Weight)  as  WeightSum  FROM  #TMPTABLE_1    
--  实现交叉功能
--
 Powred by:CHENQP
--
 Commit Date:11:22 2006-10-30        
SELECT
    V_GoodsPlaceA
= max ( case  id % 7   when   0   then  V_GoodsPlace  end ),
    I_PackageA
= max ( case  id % 7   when   0   then  I_Package  end ),
    N_WeightA
= max ( case  id % 7   when   0   then  N_Weight  end ),
    V_OutGradeA 
= max ( case  id % 7   when   0   then  V_OutGrade  end ),
    V_CustomerA 
= max ( case  id % 7   when   0   then  V_Customer  end ),
    V_PurchaseTypeA
= max ( case  id % 7   when   0   then  V_PurchaseType  end ),
    V_AreaNameA
= max ( case  id % 7   when   0   then  V_AreaName  end ),         
    
    V_GoodsPlaceB
= max ( case  id % 7   when   1   then  V_GoodsPlace  end ),
    I_PackageB
= max ( case  id % 7   when   1   then  I_Package  end ),
    N_WeightB
= max ( case  id % 7   when   1   then  N_Weight  end ),
    V_OutGradeB 
= max ( case  id % 7   when   1   then  V_OutGrade  end ),
    V_CustomerB 
= max ( case  id % 7   when   1   then  V_Customer  end ),
    V_PurchaseTypeB
= max ( case  id % 7   when   1   then  V_PurchaseType  end ),
    V_AreaNameB
= max ( case  id % 7   when   1   then  V_AreaName  end ), 

    V_GoodsPlaceC
= max ( case  id % 7   when   2   then  V_GoodsPlace  end ),
    I_PackageC
= max ( case  id % 7   when   2   then  I_Package  end ),
    N_WeightC
= max ( case  id % 7   when   2   then  N_Weight  end ),
    V_OutGradeC 
= max ( case  id % 7   when   2   then  V_OutGrade  end ),
    V_CustomerC 
= max ( case  id % 7   when   2   then  V_Customer  end ),
    V_PurchaseTypeC
= max ( case  id % 7   when   2   then  V_PurchaseType  end ),
    V_AreaNameC
= max ( case  id % 7   when   2   then  V_AreaName  end ),     

    V_GoodsPlaceD
= max ( case  id % 7   when   3   then  V_GoodsPlace  end ),
    I_PackageD
= max ( case  id % 7   when   3   then  I_Package  end ),
    N_WeightD
= max ( case  id % 7   when   3   then  N_Weight  end ),
    V_OutGradeD 
= max ( case  id % 7   when   3   then  V_OutGrade  end ),
    V_CustomerD 
= max ( case  id % 7   when   3   then  V_Customer  end ),
    V_PurchaseTypeD
= max ( case  id % 7   when   3   then  V_PurchaseType  end ),
    V_AreaNameD
= max ( case  id % 7   when   3   then  V_AreaName  end ), 

    V_GoodsPlaceE
= max ( case  id % 7   when   4   then  V_GoodsPlace  end ),
    I_PackageE
= max ( case  id % 7   when   4   then  I_Package  end ),
    N_WeightE
= max ( case  id % 7   when   4   then  N_Weight  end ),
    V_OutGradeE 
= max ( case  id % 7   when   4   then  V_OutGrade  end ),
    V_CustomerE 
= max ( case  id % 7   when   4   then  V_Customer  end ),
    V_PurchaseTypeE
= max ( case  id % 7   when   4   then  V_PurchaseType  end ),
    V_AreaNameE
= max ( case  id % 7   when   4   then  V_AreaName  end ),

    V_GoodsPlaceF
= max ( case  id % 7   when   5   then  V_GoodsPlace  end ),
    I_PackageF
= max ( case  id % 7   when   5   then  I_Package  end ),
    N_WeightF
= max ( case  id % 7   when   5   then  N_Weight  end ),
    V_OutGradeF 
= max ( case  id % 7   when   5   then  V_OutGrade  end ),
    V_CustomerF 
= max ( case  id % 7   when   5   then  V_Customer  end ),
    V_PurchaseTypeF
= max ( case  id % 7   when   5   then  V_PurchaseType  end ),
    V_AreaNameF
= max ( case  id % 7   when   5   then  V_AreaName  end ),

    V_GoodsPlaceG
= max ( case  id % 7   when   6   then  V_GoodsPlace  end ),
    I_PackageG
= max ( case  id % 7   when   6   then  I_Package  end ),
    N_WeightG
= max ( case  id % 7   when   6   then  N_Weight  end ),
    V_OutGradeG 
= max ( case  id % 7   when   6   then  V_OutGrade  end ),
    V_CustomerG 
= max ( case  id % 7   when   6   then  V_Customer  end ),
    V_PurchaseTypeG
= max ( case  id % 7   when   6   then  V_PurchaseType  end ),
    V_AreaNameG
= max ( case  id % 7   when   6   then  V_AreaName  end )
    
FROM
      (
SELECT   Top   100000   * ,
        (
SELECT   COUNT ( * FROM  #TMPTABLE_1  WHERE  IDD  <=  a.IDD) 
            
AS  id 
        
FROM  #TMPTABLE_1 a  ORDER   BY  V_GoodsPlace,I_Package ) b
        
GROUP   BY  (b.id - 1 ) / 7
        
ORDER   BY  (b.id - 1 ) / 7     
    
END



你可能感兴趣的:(笔记)