IF object_id('dbo.PCalcDynamicEmpComm','P') IS NOT NULL BEGIN DROP PROCEDURE dbo.PCalcDynamicEmpComm END GO CREATE PROCEDURE dbo.PCalcDynamicEmpComm ( @CompId BIGINT, @EmpId BIGINT ) AS BEGIN --DECLARE @EmpId BIGINT --DECLARE @CompId BIGINT --SELECT @EmpId = 0 --SELECT @CompId = FCompId FROM #Company --员工列表,只查询有特殊方案 CREATE TABLE #EmployeeScheme ( FEmpId BIGINT, FItemSchemeId BIGINT, --项目提成方案 FItemTypeKey VARCHAR(20) --累计类别方式 ) IF @EmpId != 0 BEGIN INSERT #EmployeeScheme(FEmpId,FItemSchemeId,FItemTypeKey) SELECT @EmpId,A.FId,CASE WHEN A.FAcum = 1 THEN 'ItemType' ELSE 'ItemKind' END FROM TEmployeeCommScheme AS S,TItemCommSchemeMaster AS A WHERE S.FCompId = @CompId AND S.FCompId = A.FCompId AND A.FAcum IN(1,2) AND S.FItemSchemeId = A.FId AND S.FEmpId = @EmpId END ELSE BEGIN IF object_id('tempdb..#Employee') IS NOT NULL BEGIN INSERT #EmployeeScheme(FEmpId,FItemSchemeId,FItemTypeKey) SELECT E.FEmpId,A.FId,CASE WHEN A.FAcum = 1 THEN 'ItemType' ELSE 'ItemKind' END FROM #Employee AS E,TEmployeeCommScheme AS S,TItemCommSchemeMaster AS A WHERE S.FCompId = @CompId AND A.FCompID = S.FCompId AND A.FAcum IN(1,2) AND S.FItemSchemeId = A.FId AND E.FEmpID = S.FEmpId END ELSE BEGIN INSERT #EmployeeScheme(FEmpId,FItemSchemeId,FItemTypeKey) SELECT E.FEmpId,A.FId,CASE WHEN A.FAcum = 1 THEN 'ItemType' ELSE 'ItemKind' END FROM TEmployeeSub AS E,TEmployeeCommScheme AS S,TItemCommSchemeMaster AS A WHERE E.FCompId = @CompId AND E.FCompID = S.FCompId AND E.FCompId = A.FCompId AND A.FAcum IN(1,2) AND S.FItemSchemeId = A.FId AND E.FEmpId = S.FEmpId END END IF @@ROWCOUNT = 0 BEGIN RETURN END --提成方案 CREATE TABLE #SchemeSection ( FSchemeId BIGINT NOT NULL, FSectionId BIGINT NOT NULL, FSequence INT NOT NULL, FItemTypeId BIGINT, FDispatchMode BIGINT, FSectionMode INT, FFromValue DECIMAL(18,6) NOT NULL, FToValue DECIMAL(18,6) NOT NULL, FAloneCalc BIT NOT NULL ) --获取分段 INSERT #SchemeSection(FSchemeId,FSectionId,FSequence,FItemTypeId,FDispatchMode,FSectionMode,FFromValue,FToValue,FAloneCalc) SELECT FMasterId,FId,FSequence,FItemTypeId,FDispatchMode,FSectionMode,FFromValue,FToValue,FAloneCalc FROM TItemCommSchemeSection AS A WITH(NOLOCK) WHERE EXISTS (SELECT 1 FROM #EmployeeScheme AS E WHERE E.FItemSchemeId = A.FMasterId) IF @@ROWCOUNT = 0 BEGIN RETURN END --员工类别业绩汇总 CREATE table #PerfDetailAcum ( FEmpId BIGINT NOT NULL, --员工ID FItemTypeKey VARCHAR(20) NOT NULL,--ItemType 项目类别,ItemKind 项目大类 FItemTypeId BIGINT NOT NULL, --项目类别 FDispatchMode BIGINT, --服务类别 FAmt DECIMAL(18,6) NOT NULL, --营业额 FActAmt DECIMAL(18,6) NOT NULL, --实收额 FPerf DECIMAL(18,6) NOT NULL, --虚业绩 FDedCostPerf DECIMAL(18,6) NOT NULL, --扣成本业绩 FQuantity DECIMAL(18,6) NOT NULL, --数量 ) --获取员工累计 INSERT #PerfDetailAcum(FEmpId,FItemTypeKey,FItemTypeId,FDispatchMode,FAmt,FActAmt,FPerf,FDedCostPerf,FQuantity) SELECT A.FEmpId,'ItemType',A.FItemTypeId,A.FDispatchMode,SUM(A.FAmt),SUM(FActAmt),SUM(A.FPerf),SUM(FDedCostPerf),SUM(A.FQuantity) FROM #EmpPerfDetail AS A WITH(NOLOCK) -- AND A.FWorkDate BETWEEN @FromDate AND @ToDate GROUP BY A.FEmpId,A.FItemTypeId,A.FDispatchMode IF @@ROWCOUNT = 0 BEGIN RETURN END IF EXISTS(SELECT 1 FROM #EmployeeScheme WHERE FItemTypeKey = 'ItemKind') --有按照项目大类累计的 BEGIN INSERT #PerfDetailAcum(FEmpId,FItemTypeKey,FItemTypeId,FDispatchMode,FAmt,FActAmt,FPerf,FDedCostPerf,FQuantity) SELECT A.FEmpId,'ItemKind',S.FParentId,A.FDispatchMode,SUM(A.FAmt),SUM(FActAmt),SUM(A.FPerf),SUM(FDedCostPerf),SUM(A.FQuantity) FROM #PerfDetailAcum AS A WITH(NOLOCK),TBasicType AS S WITH(NOLOCK) WHERE A.FEmpId = B.FEmpId AND A.FCompId = @CompId AND S.FId = A.FItemTypeId GROUP BY A.FEmpId,S.FParentId,A.FDispatchMode END IF EXISTS(SELECT 1 FROM #SchemeSection WHERE FItemTypeId = 1) --全部类别 BEGIN INSERT #PerfDetailAcum(FEmpId,FItemTypeKey,FItemTypeId,FDispatchMode,FAmt,FActAmt,FPerf,FDedCostPerf,FQuantity) SELECT A.FEmpId,FItemTypeKey,1,A.FDispatchMode,SUM(A.FAmt),SUM(FActAmt),SUM(A.FPerf),SUM(FDedCostPerf),SUM(A.FQuantity) FROM #PerfDetailAcum AS A WITH(NOLOCK) GROUP BY A.FEmpId,FItemTypeKey,A.FDispatchMode END IF EXISTS(SELECT 1 FROM #SchemeSection WHERE FDispatchMode = 1) BEGIN INSERT #PerfDetailAcum(FEmpId,FItemTypeKey,FItemTypeId,FDispatchMode,FAmt,FActAmt,FPerf,FDedCostPerf,FQuantity) SELECT A.FEmpId,FItemTypeKey,FItemTypeId,1,SUM(A.FAmt),SUM(FActAmt),SUM(A.FPerf),SUM(FDedCostPerf),SUM(A.FQuantity) FROM #PerfDetailAcum AS A WITH(NOLOCK) GROUP BY A.FEmpId,FItemTypeKey,FItemTypeId END declare @itemtypes table ( accumu_item_type varchar(5), join_item_type varchar(5) ) declare @actiontypes table ( accumu_action_type varchar(5), join_action_type varchar(5) ) declare @goodstypes table ( accumu_goods_type varchar(5), join_goods_type varchar(5) ) --开始循环 DECLARE @CurrEmpId BIGINT --员工ID DECLARE @SchemeId BIGINT --方案Id DECLARE @SectionId BIGINT --分段Id DECLARE @DispatchMode BIGINT --指定方式 DECLARE @SectionMode INT --分段方式 DECLARE @ItemTypeKey VARCHAR(20) --项目类别key DECLARE @ItemTypeId BIGINT --项目类别Id DECLARE @FromValue DECIMAL(18,6) --开始值 DECLARE @ToValue DECIMAL(18,6) --截止值 DECLARE @Index INT --分段遍历序号 DECLARE @Count INT --分段数 DECLARE @Amt DECIMAL(18,6) DECLARE @ActAmt DECIMAL(18,6) DECLARE @Perf DECIMAL(18,6) DECLARE @DedCostPerf DECIMAL(18,6) DECLARE @LastSectionId BIGINT --最后一次匹配成功的分段ID DECLARE @LastItemTypeId BIGINT --最后一次的项目类别 DECLARE @LastDispatchMode BIGINT --最后一次的指定方式 DECLARE @LastPerf DECIMAL(18,6) --最后一次的业绩 DECLARE @LastActAmt DECIMAL(18,6) --最后一次的实收额 DECLARE @LastAmt DECIMAL(18,6) --最后一次的营业额 DECLARE @LastDedCostPerf DECIMAL(18,6) --最后一次的扣成本业绩 --遍历员工 DECLARE EmpCur CURSOR FOR SELECT FEmpId,FItemSchemeId,FItemTypeKey FROM #EmployeeScheme AS E WHERE EXISTS(SELECT 1 FROM #PerfDetailAcum AS A WHERE A.FEmpId = E.FEmpId) OPEN EmpCur FETCH EmpCur INTO @CurrEmpId,@SchemeId,@ItemTypeKey WHILE @@FETCH_STATUS = 0 BEGIN print @CurrEmpId SELECT @LastSectionId = 0, @LastItemTypeId = -1, @LastDispatchMode = -1, @Amt = 0, @ActAmt = 0, @Perf = 0, @DedCostPerf = 0 SELECT @Count = COUNT(1) FROM #SchemeSection WHERE FSchemeId = @SchemeId SELECT @Index = @Count --从下往上匹配 WHILE @Index > 0 BEGIN SELECT @SectionId = FSectionId, @ItemTypeId = FItemTypeId, @DispatchMode = FDispatchMode, @SectionMode = FSectionMode, @FromValue = FFromValue, @ToValue = FToValue FROM #SchemeSection WHERE FSchemeId = @SchemeId AND FSequence = @Index PRINT 'SectoinId=' + CAST(@SectionId AS VARCHAR(20) ) + ',ItemTypeId='+ CAST(@ItemTypeId AS VARCHAR(20)) + ',DispatchMode='+ CAST(@DispatchMode AS VARCHAR(20)) + ',SectionMode='+ CAST(@SectionMode AS VARCHAR(20)) + ',FromValue='+ CAST(@FromValue AS VARCHAR(20)) + ',ToValue='+ CAST(@ToValue AS VARCHAR(20)) IF @LastItemTypeId != @ItemTypeId OR @LastDispatchMode != @DispatchMode BEGIN PRINT 'SELECT' SELECT @Amt = FAmt, @ActAmt = FActAmt, @Perf = FPerf, @DedCostPerf = FDedCostPerf FROM #PerfDetailAcum WHERE FEmpId = @CurrEmpId AND FItemTypeKey = @ItemTypeKey AND FItemTypeId = @ItemTypeId AND FDispatchMode = @DispatchMode -- SELECT * FROM #PerfDetailAcum WHERE FEmpId = @CurrEmpId AND FItemTypeId = @ItemTypeId END PRINT '@Amt=' + CAST(@Amt AS VARCHAR(20) ) + ',@ActAmt='+ CAST(@ActAmt AS VARCHAR(20)) + ',@@Perf='+ CAST(@Perf AS VARCHAR(20)) + ',@@DedCostPerf='+ CAST(@DedCostPerf AS VARCHAR(20)) DECLARE @Value DECIMAL(18,6) IF @SectionMode = 2 --营业额 BEGIN SELECT @Value = @Amt END ELSE IF @SectionMode = 3 --实际收额 BEGIN SELECT @Value = @ActAmt END ELSE IF @SectionMode = 4 --业绩 BEGIN SELECT @Value = @Perf END ELSE IF @SectionMode = 5 --扣成本业绩 BEGIN SELECT @Value = @DedCostPerf END ELSE BEGIN SELECT @Value = 0 END IF @Value >= @FromValue AND @Value < @ToValue BEGIN --匹配到了 --获取提成方式 -- IF EXISTS(SELECT 1 FROM TItemCommSchemeDetail AS A WITH(NOLOCK) WHERE FParentId = @SectionId AND A.FItemId = 0) BEGIN UPDATE A SET A.FBaseCommMode = B.FCommMode, A.FBaseCommValue = B.FCommValue FROM #EmpPerfDetail AS A,TItemCommSchemeDetail AS B WITH(NOLOCK) WHERE B.FPayTypeId = 0 AND A.FWorkType = B.FWorkType AND A.FDispatchMode = B.FDispatchMode AND B.FParentId = @SectionId AND A.FCategory = 'Item' END UPDATE A SET A.FBaseCommMode = B.FCommMode, A.FBaseCommValue = B.FCommValue FROM #EmpPerfDetail AS A,TItemCommSchemeDetail AS B WITH(NOLOCK) WHERE B.FPayTypeId = 0 AND A.FItemId = B.FItemId AND A.FWorkType = B.FWorkType AND A.FDispatchMode = B.FDispatchMode AND B.FParentId = @SectionId AND A.FCategory = 'Item' BREAK; END SELECT @Index = @Index - 1 END FETCH EmpCur INTO @CurrEmpId,@SchemeId,@ItemTypeKey END CLOSE EmpCur DEALLOCATE EmpCur --计算提成 UPDATE A SET A.FBaseComm = CASE A.FBaseCommMode WHEN 1 THEN A.FBaseCommValue * A.FSplitQuantity * A.FShareRate WHEN 2 THEN A.FBaseCommValue * A.FAmt WHEN 3 THEN A.FBaseCommValue * A.FActAmt WHEN 4 THEN A.FBaseCommValue * A.FPerf WHEN 5 THEN A.FBaseCommValue * A.FDedCostPerf WHEN 6 THEN A.FBaseCommValue * A.FPerf * A.FActRate --业绩乘以实收额比率 WHEN 7 THEN A.FBaseCommValue * A.FOrigPrice ELSE 0 END FROM #EmpPerfDetail AS A WHERE A.FCategory = 'Item' UPDATE A SET A.FComm = A.FBaseComm + A.FExtraComm FROM #EmpPerfDetail AS A WHERE A.FCategory = 'Item' END GO IF object_id('dbo.PRptWageReport01','P') IS NOT NULL BEGIN DROP PROCEDURE dbo.PRptWageReport01 END GO CREATE PROCEDURE PRptWageReport01 ( @IParam VARCHAR(8000) ) AS BEGIN DECLARE @CompanyStr VARCHAR(8000) DECLARE @FromDate CHAR(10) DECLARE @ToDate CHAR(10) DECLARE @IncChild CHAR DECLARE @CashierId BIGINT DECLARE @DeptId BIGINT DECLARE @AllCompany BIT DECLARE @PayTypeNo VARCHAR(1000) DECLARE @ItemNo VARCHAR(1000) DECLARE @DeptNo VARCHAR(1000) DECLARE @CompId BIGINT SELECT @CompanyStr = dbo.FNGetParamValue(@IParam,'Company') SELECT @FromDate = CONVERT(CHAR(10),dbo.FNGetParamValue(@IParam,'FromDate'),23) SELECT @ToDate = CONVERT(CHAR(10),dbo.FNGetParamValue(@IParam,'ToDate'),23) SELECT @PayTypeNo = ISNULL(dbo.FNGetParamValue(@IParam,'PayTypeNo'),'') SELECT @ItemNo = ISNULL(dbo.FNGetParamValue(@IParam,'ItemNo'),'') SELECT @DeptNo = ISNULL(dbo.FNGetParamValue(@IParam,'DeptNo'),'') IF ISNULL(@ToDate,'') = '' OR ISNULL(@FromDate,'') = '' OR @FromDate > @ToDate BEGIN RETURN END CREATE TABLE #Company ( FCompId BIGINT, FCompNo VARCHAR(20) ) INSERT #Company SELECT A.FId,A.FNo FROM TCompany AS A WITH(NOLOCK), dbo.FNStrSplit(@CompanyStr) AS B WHERE A.FNo = B.Field SELECT @CompId = FCompId FROM #Company IF @DeptNo = '' OR @DeptNo = '*' BEGIN SET @DeptId = 0 END SELECT @DeptId = FId FROM TBasicType WITH(NOLOCK) WHERE FKey = 'Department' AND FNo = @DeptNo DECLARE @PayTypeIds TABLE ( FPayTypeId BIGINT ) DECLARE @ItemIds TABLE ( FItemId BIGINT ) DECLARE @CourseIds TABLE ( FCourseId BIGINT ) IF @PayTypeNo != '' BEGIN INSERT @PayTypeIds(FPayTypeId) SELECT A.FId FROM TPayAccount AS A WITH(NOLOCK), dbo.FNStrSplit(@PayTypeNo) AS B WHERE A.FNo = B.Field END IF @ItemNo != '' BEGIN INSERT @ItemIds(FItemId) SELECT A.FId FROM TItem AS A WITH(NOLOCK), dbo.FNStrSplit(@ItemNo) AS B WHERE A.FNo = B.Field INSERT @CourseIds(FCourseId) SELECT A.FId FROM TCourse AS A WITH(NOLOCK) ,@ItemIds AS B WHERE A.FItemId = B.FItemId END SET @AllCompany = 0 --生成#Company 中的所有员工业绩 EXEC PGenerateEmpPerf @AllCompany,@FromDate,@ToDate /* SELECT A.FId AS FEmpId , A.FNo FEmpNo, A.FName AS FEmpName, CAST(A.FPosId AS VARCHAR(20)) AS FPosId FROM TEmployee AS A WITH(NOLOCK) INNER JOIN #Company AS C on c.FCompId = a.FCompId UNION ALL */ SELECT A.FId AS FEmpId, A.FNo AS FEmpNo,A.FName AS FEmpName, CAST(A.FPosId AS VARCHAR(20)) AS PosId INTO #Employee FROM TEmployee AS A WITH(NOLOCK) INNER JOIN TEmployeeSub AS B ON B.FEmpId = A.FId INNER JOIN #Company AS C ON C.FCompId = B.FCompId WHERE (@DeptId = 0 OR A.FDeptId = @DeptId) --缓存到临时表 SELECT A.* INTO #EmpPerfDetail FROM TEmpPerfDetail# AS A WITH(NOLOCK),#Employee AS E WHERE A.FIsExpired = 0 AND A.FEmpId = E.FEmpId AND A.FCompId = @CompId AND A.FWorkDate BETWEEN @FromDate AND @ToDate --计算动态提成 EXEC PCalcDynamicEmpComm @CompId,0 SELECT E.FEmpId AS EmpId, E.FEmpNo AS EmpNo, A.FCategory AS Category, FBillNo AS BillNo,FBillDetailId AS BillDetailId, CONVERT(varchar(20),A.FBillTime, 120) AS BillTime,FWorkDate AS WorkDate, FWorkType AS WorkType,FDispatchMode AS DispatchMode, S.FName AS DispatchModeName, FItemNo AS ItemNo, FItemNo + '-' + FItemName AS ItemName, FQuantity AS Quantity, P.FName AS PayTypeName, A.FPayTypeId AS PayTypeId, FAmt AS PayAmt, FPerf AS Perf,FComm AS Comm,FDedCostPerf AS DedCostPerf, CASE WHEN A.FCategory = 'Item' THEN S2.FName WHEN A.FCategory = 'Goods' THEN S4.FName ELSE S3.FName END AS WorkTypeName, A.FMemNo AS MemNo,ISNULL(A.FCustName,M.FName) AS CustName, A.FCardNo AS CardNo, CASE WHEN ISNULL(FCustGender,M.FGender) = 1 THEN '男' ELSE '女' END AS CustGender INTO #PerfDetail FROM #Employee AS E, #EmpPerfDetail AS A LEFT JOIN TBasicType AS S WITH(NOLOCK) ON S.FKey = 'ServeDispatchMode' AND S.FId = A.FDispatchMode LEFT JOIN TBasicType AS S2 WITH(NOLOCK) ON S2.FKey = 'ServeWorkType' AND S2.FId = A.FWorkType AND A.FCategory = 'Item' LEFT JOIN TBasicType AS S3 WITH(NOLOCK) ON S3.FKey = 'MemSaleWorkType' AND S3.FId = A.FWorkType AND A.FCategory IN('CardSale','CardRchg','CourseSale','CoursePackSale') LEFT JOIN TBasicType AS S4 WITH(NOLOCK) ON S4.FKey = 'GoodsSaleWorkType' AND S4.FId = A.FWorkType AND A.FCategory IN('Goods') LEFT JOIN TMember AS M WITH(NOLOCK) ON A.FMemId = M.FMemId LEFT JOIN TPayAccount AS P ON P.FId = A.FPayTypeId WHERE E.FEmpId = A.FEmpId AND (@PayTypeNo = '' OR A.FPayTypeId IN(SELECT FPayTypeId FROM @PayTypeIds)) AND (@ItemNo = '' OR (A.FCategory = 'Item' AND A.FItemId IN(SELECT FItemId FROM @ItemIds) ) OR (A.FCategory = 'CourseSale' AND A.FItemId IN(SELECT FCourseId FROM @CourseIds) )) ORDER BY A.FEmpId,CASE WHEN A.FCategory IN('CardSale','CardRchg') THEN 1 WHEN A.FCategory IN( 'CourseSale','CoursePackSale') THEN 2 WHEN A.FCategory = 'Goods' THEN 3 WHEN A.FCategory = 'Item' THEN 4 ELSE 5 END ,FBillTime, FBillNo,FBillDetailId,FWorkType SELECT FEmpNo AS EmpNo, FEmpName AS EmpName FROM #Employee AS B WHERE EXISTS(SELECT TOP 1 1 FROM #PerfDetail AS A WHERE A.EmpId = B.FEmpId) ORDER BY FEmpNo SELECT * FROM #PerfDetail --汇总 SELECT A.EmpId, A.Category ,SumType = CAST('Pay' AS VARCHAR(10)),PayTypeId, SUM(A.PayAmt) AS PayAmt,SUM(DedCostPerf) AS DedCostPerf,SUM(A.Perf) AS Perf,SUM(A.Comm) AS Comm ,CAST(0 AS DECIMAL(18,6)) AS Quantity INTO #Summary FROM #PerfDetail AS A,#Employee AS E--,#Company AS C WHERE A.EmpId = E.FEmpId -- AND A.CompId = C.FCompId AND A.WorkDate BETWEEN @FromDate AND @ToDate -- AND A.FIsExpired = 0 -- AND (@PayTypeNo = '' OR A.FPayTypeId IN(SELECT FPayTypeId FROM @PayTypeIds)) -- AND (@ItemNo = '' OR (A.FCategory = 'Item' AND A.FItemId IN(SELECT FItemId FROM @ItemIds) )) GROUP BY A.EmpId, A.Category ,A.PayTypeId INSERT #Summary(EmpId,Category,SumType,PayAmt,DedCostPerf,Perf,Comm) SELECT EmpId,Category, 'Total',SUM(PayAmt),SUM(DedCostPerf),SUM(Perf),SUM(Comm) FROM #Summary GROUP BY EmpId,Category INSERT #Summary(EmpId,Category,SumType,PayAmt,DedCostPerf,Perf,Comm) SELECT EmpId,'All', 'Total',SUM(PayAmt),SUM(DedCostPerf),SUM(Perf),SUM(Comm) FROM #Summary WHERE SumType = 'Total' GROUP BY EmpId UPDATE A SET A.Quantity = T2.Quantity FROM #Summary AS A, ( SELECT EmpId,Category,SUM(Quantity) AS Quantity FROM ( SELECT A.EmpId,Category,1 AS Quantity FROM #PerfDetail AS A,#Employee AS E,#Company AS C WHERE A.EmpId = E.FEmpId GROUP BY A.EmpId,A.Category,A.BillNo,A.BillDetailId,A.WorkType ) AS T GROUP BY T.EmpId,Category ) AS T2 WHERE T2.Category = A.Category AND T2.EmpId = A.EmpId AND A.SumType = 'Total' SELECT E.FEmpNo AS EmpNo,A.Category AS Category, A.SumType AS SumType, ISNULL(P.FName,'') AS PayTypeName, ISNULL(A.PayAmt,0) AS PayAmt,ISNULL(A.Perf,0) AS Perf,ISNULL(A.DedCostPerf,0) AS DedCostPerf,ISNULL(A.Quantity,0) AS Quantity,ISNULL(A.Comm,0) AS Comm FROM #Employee AS E,#Summary AS A LEFT JOIN TPayAccount AS P ON P.FId = A.PayTypeId WHERE A.EmpId = E.FEmpId ORDER BY CASE WHEN ISNULL(P.FNo,'') = '' THEN '总' ELSE P.FNo END END GO IF object_id('dbo.PGenerateEmpPerf','P') IS NOT NULL BEGIN DROP PROCEDURE dbo.PGenerateEmpPerf END GO CREATE PROCEDURE dbo.PGenerateEmpPerf ( @AllCompany BIT, @FromDate CHAR(10), @ToDate CHAR(10) ) AS BEGIN DECLARE @Error INT EXEC @Error = PGenerateEmpPerfDetail @AllCompany,@FromDate,@ToDate IF @Error != 0 BEGIN RETURN -1 END EXEC @Error = PGenerateEmpPerfFinish @AllCompany,@FromDate,@ToDate IF @Error != 0 BEGIN RETURN -1 END RETURN 0 END GO IF object_id('dbo.PRptWageReport02','P') IS NOT NULL BEGIN DROP PROCEDURE dbo.PRptWageReport02 END GO CREATE PROCEDURE PRptWageReport02 ( @IParam VARCHAR(8000) ) AS BEGIN /* EXEC dbo.PDropTempTables DECLARE @IParam VARCHAR(8000) SET @IParam='FromDate="2014-01-17"ToDate="2015-01-17"FromEmpNo=""ToEmpNo=""FromItemTypeNo=""ToItemTypeNo=""CompNo="A001"IncChild="1"' */ DECLARE @CompId BIGINT DECLARE @FromDate CHAR(10) DECLARE @ToDate CHAR(10) DECLARE @EmployeeText VARCHAR(1000) DECLARE @ItemTypeNo VARCHAR(1000) DECLARE @ItemKindNo VARCHAR(1000) DECLARE @IncChild CHAR DECLARE @CashierId BIGINT DECLARE @DeptId BIGINT DECLARE @ShowAmt INT DECLARE @DeptNo VARCHAR(30) SELECT @CompId = dbo.FNGetParamCompId(@IParam) SELECT @FromDate = CONVERT(CHAR(10),dbo.FNGetParamValue(@IParam,'FromDate'),23) SELECT @ToDate = CONVERT(CHAR(10),dbo.FNGetParamValue(@IParam,'ToDate'),23) SELECT @EmployeeText = ISNULL(dbo.FNGetParamValue(@IParam,'Employees'),'') SELECT @ItemTypeNo = ISNULL(dbo.FNGetParamValue(@IParam,'ItemTypeNo'),'') SELECT @ItemKindNo = ISNULL(dbo.FNGetParamValue(@IParam,'ItemKindNo'),'') SELECT @ShowAmt = ISNULL(dbo.FNGetParamValue(@IParam,'ShowAmt'),0) SELECT @DeptNo = ISNULL(dbo.FNGetParamValue(@IParam,'DeptNo'),'') -- SELECT @ToItemTypeNo = dbo.FNGetParamValue(@IParam,'ToItemTypeNo') IF ISNULL(@CompId,0) = 0 OR ISNULL(@ToDate,'') = '' OR ISNULL(@FromDate,'') = '' OR @FromDate > @ToDate BEGIN RETURN END IF @DeptNo = '' OR @DeptNo = '*' BEGIN SET @DeptId = 0 END SELECT @DeptId = FId FROM TBasicType WITH(NOLOCK) WHERE FKey = 'Department' AND FNo = @DeptNo DECLARE @Employees TABLE ( FNo VARCHAR(30) ) INSERT @Employees SELECT Field FROM dbo.FNStrSplit(@EmployeeText) --初始化员工表 --生成员工列表 SELECT A.FId AS FEmpId,FNo AS FEmpNo ,A.FName AS FEmpName INTO #Employee FROM TEmployee AS A WITH(NOLOCK) INNER JOIN TEmployeeSub AS B ON B.FEmpId = A.FId AND B.FCompId = @CompId -- LEFT JOIN TEmployeeSub AS B WITH(NOLOCK) ON B.FEmpId=A.FId WHERE A.FId != 0 AND B.FCompID = @CompId AND (@DeptId = 0 OR A.FDeptId = @DeptId) --AND (A.FCompId=@CompId OR B.FCompId=@CompId) -- AND (@EmployeeText = '' OR B.FEmpNo IN (select FNo from @Employees ) -- OR A.FNo IN (select FNo from @Employees) ) AND (@EmployeeText = '' OR A.FNo IN (select FNo from @Employees) ) /* IF @@ROWCOUNT = 0 BEGIN RETURN END */ CREATE TABLE #Company ( FCompId BIGINT, FCompNo VARCHAR(20) ) INSERT #Company (FCompID,FCompNo) VALUES(@CompId,'') DECLARE @Error INT EXEC @Error = PGenerateEmpPerf 0,@FromDate,@ToDate IF @Error != 0 BEGIN RETURN -1 END --初始化项目类别表 SELECT A.FId AS FTypeId, REPLACE(A.FNo,'.','.') AS FTypeNo, FName AS FTypeName, A.FParentId AS FParentId, 'ItemType' AS FCategory INTO #ItemType FROM TBasicType AS A WITH(NOLOCK) LEFT JOIN TBasicTypeSub AS B WITH(NOLOCK) ON B.FBasicTypeId = A.FId AND B.FCompId = @CompId WHERE A.FKey = 'ItemType' AND (@ItemTypeNo = '' OR A.FNo IN (SELECT Field FROM dbo.FNStrSplit(@ItemTypeNo))) AND ((@CompId = 1 AND A.FEnabled = 1 ) OR ISNULL(B.FEnabled,1) = 1) GROUP BY A.FId,A.FNo,A.FName,A.FParentId UNION ALL --初始化项目分组别表 SELECT A.FId AS FTypeId, REPLACE(A.FNo,'.','.') AS FTypeNo, FName AS FTypeName, CAST(0 AS BIGINT) AS FParentId, 'ItemKind' AS FCategory -- INTO #ItemKind FROM TBasicType AS A WITH(NOLOCK) WHERE A.FKey = 'ItemKind' AND (@ItemKindNo = '' OR A.FNo IN (SELECT Field FROM dbo.FNStrSplit(@ItemKindNo))) GROUP BY A.FId,A.FNo,A.FName CREATE TABLE #Total ( FEmpId BIGINT, --员工ID FType VARCHAR(20), --类别 FQty FLOAT, --数量 FAmt FLOAT --业绩 ) CREATE TABLE #ItemDetail ( FEmpId BIGINT, --员工ID FItemTypeId BIGINT, --项目类别Id FCagegory VARCHAR(10) DEFAULT '', --分类级别 'ItemType','ItemKind' FTotalAmt FLOAT DEFAULT 0, --总业绩 FTotalQty FLOAT DEFAULT 0, --总数量 FCashAmt FLOAT DEFAULT 0, --现金业绩 FCardAmt FLOAT DEFAULT 0, --卡付业绩 FOtherAmt FLOAT DEFAULT 0, --其他业绩 FCashQty FLOAT DEFAULT 0, --现金数 FCardQty FLOAT DEFAULT 0, --卡付数 FOtherQty FLOAT DEFAULT 0, --其他数 FDMQty1 FLOAT DEFAULT 0, --派遣方式1数量 FDMAmt1 FLOAT DEFAULT 0, --方式1 业绩 FDMQty2 FLOAT DEFAULT 0, -- FDMAmt2 FLOAT DEFAULT 0, FDMQty3 FLOAT DEFAULT 0, FDMAmt3 FLOAT DEFAULT 0, FDMQty4 FLOAT DEFAULT 0, FDMAmt4 FLOAT DEFAULT 0, FWorkQty1 FLOAT DEFAULT 0, --岗位1 FWorkAmt1 FLOAT DEFAULT 0, --岗位1 业绩 FWorkQty2 FLOAT DEFAULT 0, -- FWorkAmt2 FLOAT DEFAULT 0, FWorkQty3 FLOAT DEFAULT 0, FWorkAmt3 FLOAT DEFAULT 0, FWorkQty4 FLOAT DEFAULT 0, FWorkAmt4 FLOAT DEFAULT 0, ) --把所有符合条件的数据拿到临时表中 #EmpPerfDetail SELECT A.*,CAST('' AS VARCHAR(20)) AS FPayTypeCatg /*支付大类(现金,卡,其他)*/ INTO #EmpPerfDetail FROM TEmpPerfDetail# AS A, #Employee AS E,#Company AS C WHERE A.FWorkDate BETWEEN @FromDate AND @ToDate AND C.FCompId = A.FCompId AND A.FEmpId = E.FEmpId AND A.FIsExpired = 0 --计算动态算法业绩 EXEC PCalcDynamicEmpComm @CompId,0 --金额显示为 IF @ShowAmt = 1 BEGIN UPDATE #EmpPerfDetail SET FPerf = FAmt END ELSE IF @ShowAmt = 2 BEGIN UPDATE #EmpPerfDetail SET FPerf = FDedCostPerf END --设置支付大类 UPDATE A SET A.FPayTypeCatg = CASE WHEN A.FPayTypeId IN(1,2) THEN 'Cash' --现金 WHEN (A.FPayTypeId IN(6) OR ISNULL(S.FIsAccount,0) = 1) THEN 'Card' --卡付 ELSE 'Other' END --支付大类, 2 现金,1 卡付, 3其他 FROM #EmpPerfDetail AS A,TPayAccount AS S WITH(NOLOCK) WHERE A.FPayTypeId = S.FId --更新项目类别 -- DEBUG --有待改进 UPDATE A SET A.FItemTypeId = B.FTypeId FROM #EmpPerfDetail AS A, TItem AS B WITH(NOLOCK) WHERE A.FItemId = B.FId AND A.FCategory = 'Item' --更新项目数量,因为有支付拆分的情况 --所有的收银单据,每个员工一行 SELECT A.FEmpId AS FEmpId, A.FBillId AS FBillId, MAX(FCustQuantity) AS FCustQuantity, --客数 CASE WHEN (MIN(A.FCustGender)) = 1 THEN 'ItemMale' ELSE 'ItemFemale' END AS FCustGender, --客户性别 CASE WHEN (MIN(A.FCustNewOld)) = 0 THEN 'ItemNew' ELSE 'ItemOld' END AS FCustNewOld, --新客老客 CASE WHEN (MIN(CASE WHEN A.FDispatchMode = 10001 THEN 1 ELSE 2 END)) = 1 THEN 'ItemDMode1' ELSE 'ItemDMode2' END AS FDispatchMode, --1指定,2轮牌 'Item' + MIN(A.FPayTypeCatg) AS FPayCatg --支付大类, 2 现金,1 卡付, 3其他 INTO #ItemBill FROM #EmpPerfDetail AS A LEFT JOIN TPayAccount AS S WITH(NOLOCK) ON S.FId = A.FPayTypeId WHERE FCategory = 'Item' GROUP BY A.FEmpId,A.FBillId --项目汇总 INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT A.FEmpId,A.FCagegory, SUM(A.FQty) AS FQty, SUM(A.FAmt) AS FAmt FROM ( --总客数 SELECT FEmpId,'ItemTotal' AS FCagegory, SUM(FCustQuantity) AS FQty, 0 AS FAmt FROM #ItemBill GROUP BY FEmpId UNION ALL --总业绩 SELECT FEmpId,'ItemTotal' AS FCagegory, CAST(0 AS DECIMAL(18,6)) AS FQty, SUM(FPerf) AS FAmt FROM #EmpPerfDetail WHERE FCategory = 'Item' GROUP BY FEmpId UNION ALL --指定客数,和不指定客数 SELECT FEmpId,FDispatchMode AS FCagegory,SUM(FCustQuantity) AS FQty, 0 AS FAmt FROM #ItemBill GROUP BY FEmpId, FDispatchMode UNION ALL --每种方式的业绩, SELECT FEmpId, CASE FDispatchMode WHEN 10001 THEN 'ItemDMode1' WHEN 10002 THEN 'ItemDMode2' WHEN 10003 THEN 'ItemDMode3' WHEN 10004 THEN 'ItemDMode4' ELSE '' END, 0 AS FQty,SUM(FPerf) AS FAmt FROM #EmpPerfDetail WHERE FCategory = 'Item' GROUP BY FEmpId, FDispatchMode --岗位业绩 UNION ALL SELECT FEmpId, CASE FWorkType WHEN 11001 THEN 'ItemWork1' WHEN 11002 THEN 'ItemWork2' WHEN 11003 THEN 'ItemWork3' WHEN 11004 THEN 'ItemWork4' ELSE '' END, SUM(FSplitQuantity) AS FQty,SUM(FPerf) AS FAmt FROM #EmpPerfDetail WHERE FCategory = 'Item' GROUP BY FEmpId, FWorkType UNION ALL --男女客数量 SELECT FEmpId, FCustGender AS FType, SUM(FCustQuantity) AS FQty, 0 AS FAmt FROM #ItemBill GROUP BY FEmpId, FCustGender UNION ALL --男客业绩 SELECT FEmpId, CASE FCustGender WHEN 1 THEN 'ItemMale' ELSE 'ItemFemale' END AS FType, 0 AS FQty, SUM(FPerf) AS FAmt FROM #EmpPerfDetail WHERE FCategory = 'Item' GROUP BY FEmpId, FCustGender UNION ALL --新老客数量 SELECT FEmpId, FCustNewOld AS FType, SUM(FCustQuantity) AS FQty, 0 AS FAmt FROM #ItemBill GROUP BY FEmpId, FCustNewOld UNION ALL --新老客业绩 SELECT FEmpId, CASE FCustNewOld WHEN 0 THEN 'ItemNew' ELSE 'ItemOld' END AS FType, 0 AS FQty, SUM(FPerf) AS FAmt FROM #EmpPerfDetail WHERE FCategory = 'Item' GROUP BY FEmpId, FCustNewOld --新客老客,暂时没有 UNION ALL --现,卡付,其他支付客数 SELECT FEmpId, FPayCatg AS FType, SUM(FCustQuantity) AS FQty, 0 AS FAmt FROM #ItemBill GROUP BY FEmpId, FPayCatg UNION ALL --现金,卡,其他支付业绩 SELECT A.FEmpId, 'Item' + A.FPayTypeCatg, 0 AS FQty, SUM(A.FPerf) AS FAmt FROM #EmpPerfDetail AS A WHERE A.FCategory = 'Item' GROUP BY A.FEmpId,A.FPayTypeCatg ) AS A GROUP BY A.FEmpId,A.FCagegory --产品汇总 INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT A.FEmpId , A.FType, SUM(A.FQty) AS FQty , SUM(A.FAmt) AS FAmt FROM ( --产品总计 SELECT A.FEmpId, 'GoodsTotal' AS FType, SUM(A.FQuantity) AS FQty, SUM(A.FPerf) AS FAmt FROM ( --有一个产品拆分支付的情况,数量只能取一条 SELECT B.FEmpId, B.FBillDetailId, B.FQuantity, SUM(B.FPerf) AS FPerf FROM #EmpPerfDetail AS B WHERE B.FCategory = 'Goods' GROUP BY B.FEmpId, B.FBillDetailId, B.FQuantity ) AS A GROUP BY A.FEmpId UNION ALL --产品各种支付方式 SELECT A.FEmpId, 'Goods' + A.FPayTypeCatg AS FType, SUM(A.FQuantity) AS FQty, SUM(A.FPerf) AS FAmt FROM #EmpPerfDetail AS A WHERE A.FCategory = 'Goods' GROUP BY A.FEmpId,A.FPayTypeCatg ) AS A GROUP BY A.FEmpId,A.FType --卡汇总 INSERT #Total(FEmpId,FType,FQty,FAmt) --卖卡和充值 SELECT FEmpId, FCategory, COUNT(1) AS FQty, SUM(Amt) AS FAmt FROM ( SELECT FEmpId, FCategory, 1 AS FQty, SUM(FPerf) AS Amt FROM #EmpPerfDetail WHERE FCategory IN ('CardSale','CardRchg') GROUP BY FEmpId,FCategory,FBillId ) AS A GROUP BY FEmpId, FCategory --卖卡和充值汇总 INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT FEmpId, 'CardTotal', SUM(FQty) AS FQty, SUM(FAmt) AS FAmt FROM #Total WHERE FType IN('CardSale','CardRchg') GROUP BY FEmpId --疗程和套盒 INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT FEmpId,FCategory, SUM(FQty) AS FQty, SUM(FAmt) AS FAmt FROM( SELECT FEmpId, 'CourseSale' AS FCategory, FQuantity AS FQty, SUM(FPerf) AS FAmt FROM #EmpPerfDetail WHERE FCategory IN( 'CourseSale','CoursePackSale') GROUP BY FEmpId,FBillId,FBillDetailId,FQuantity,FCategory ) as A GROUP BY FEmpId,FCategory --按项目类别分组汇总 INSERT #ItemDetail(FEmpId,FCagegory,FItemTypeId, FTotalAmt, FTotalQty, FCashAmt, FCardAmt, FOtherAmt, FCashQty, FCardQty, FOtherQty, FDMQty1, FDMAmt1, FDMQty2, FDMAmt2, FDMQty3,FDMAmt3,FDMQty4,FDMAmt4, FWorkQty1, FWorkAmt1, FWorkQty2, FWorkAmt2, FWorkQty3,FWorkAmt3,FWorkQty4,FWorkAmt4) SELECT A.FEmpId, A.FCagegory, A.FItemTypeId,SUM(FTotalAmt), SUM(FTotalQty), SUM(FCashAmt), SUM(FCardAmt), SUM(FOtherAmt), SUM(FCashQty), SUM(FCardQty), SUM(FOtherQty), SUM(FDMQty1), SUM(FDMAmt1), SUM(FDMQty2), SUM(FDMAmt2), SUM(FDMQty3),SUM(FDMAmt3),SUM(FDMQty4),SUM(FDMAmt4), SUM(FWorkQty1), SUM(FWorkAmt1), SUM(FWorkQty2), SUM(FWorkAmt2), SUM(FWorkQty3),SUM(FWorkAmt3),SUM(FWorkQty4),SUM(FWorkAmt4) FROM ( --每种项目类别总计 SELECT A.FEmpId ,'ItemType' AS FCagegory,A.FItemTypeId, SUM(A.FQuantity) AS FTotalQty, SUM(FPerf) AS FTotalAmt, 0 AS FCashQty, 0 AS FCashAmt, 0 AS FCardQty, 0 AS FCardAmt, 0 AS FOtherQty, 0 AS FOtherAmt, 0 AS FDMQty1, 0 AS FDMAmt1,0 AS FDMQty2, 0 AS FDMAmt2,0 AS FDMQty3, 0 AS FDMAmt3,0 AS FDMQty4, 0 AS FDMAmt4, 0 AS FWorkQty1, 0 AS FWorkAmt1,0 AS FWorkQty2, 0 AS FWorkAmt2,0 AS FWorkQty3, 0 AS FWorkAmt3,0 AS FWorkQty4, 0 AS FWorkAmt4 FROM ( SELECT FEmpId,FItemTypeId,FQuantity,SUM(FPerf) AS FPerf FROM #EmpPerfDetail WHERE FCategory = 'Item' GROUP BY FEmpId,FItemTypeId,FBillDetailId, FQuantity ) AS A GROUP BY FEmpId,FItemTypeId UNION ALL --每种项目类别按支付统计 ,现金,卡,其他, SELECT FEmpId, 'ItemType' AS FCagegory, FItemTypeId, 0 AS FTotalQty, 0 AS FTotalAmt, SUM(CASE WHEN FPayTypeCatg = 'Cash' THEN FQuantity ELSE 0 END) AS FCashQty, SUM(CASE WHEN FPayTypeCatg = 'Cash' THEN FPerf ELSE 0 END) AS FCashAmt, SUM(CASE WHEN FPayTypeCatg = 'Card' THEN FQuantity ELSE 0 END) AS FCardQty, SUM(CASE WHEN FPayTypeCatg = 'Card' THEN FPerf ELSE 0 END) AS FCardAmt, SUM(CASE WHEN FPayTypeCatg = 'Other' THEN FQuantity ELSE 0 END) AS FOtherQty, SUM(CASE WHEN FPayTypeCatg = 'Other' THEN FPerf ELSE 0 END) AS FOtherAmt, 0 AS FDMQty1, 0 AS FDMAmt1,0 AS FDMQty2, 0 AS FDMAmt2,0 AS FDMQty3, 0 AS FDMAmt3,0 AS FDMQty4, 0 AS FDMAmt4, 0 AS FWorkQty1, 0 AS FWorkAmt1,0 AS FWorkQty2, 0 AS FWorkAmt2,0 AS FWorkQty3, 0 AS FWorkAmt3,0 AS FWorkQty4, 0 AS FWorkAmt4 FROM ( --可能有一个项目同一个人服务两个岗位的情况,数量只能算一个 SELECT FEmpId, FBillDetailId, FItemTypeId, FQuantity,FPayTypeCatg, SUM(FPerf) AS FPerf FROM #EmpPerfDetail WHERE FCategory = 'Item' GROUP BY FEmpId, FBillDetailId, FItemTypeId, FQuantity,FPayTypeCatg --SELECT * FROM #EmpPerfDetail WHERE FCategory = 'Item' AND FBillDetailId = 5036650046547848466 ) AS A GROUP BY A.FEmpId,A.FItemTypeId UNION ALL --每种项目类别按服务方式统计, 点,轮,... SELECT FEmpId,'ItemType' AS FCagegory, FItemTypeId, 0 AS FTotalQty, 0 AS FTotalAmt, 0 AS FCashQty, 0 AS FCashAmt, 0 AS FCardQty, 0 AS FCardAmt, 0 AS FOtherQty, 0 AS FOtherAmt, SUM(CASE WHEN FDispatchMode = 10001 THEN FQuantity ELSE 0 END) AS FDMQty1, SUM(CASE WHEN FDispatchMode = 10001 THEN FPerf ELSE 0 END) AS FDMAmt1, SUM(CASE WHEN FDispatchMode = 10002 THEN FQuantity ELSE 0 END) AS FDMQty2, SUM(CASE WHEN FDispatchMode = 10002 THEN FPerf ELSE 0 END) AS FDMAmt2, SUM(CASE WHEN FDispatchMode = 10003 THEN FQuantity ELSE 0 END) AS FDMQty3, SUM(CASE WHEN FDispatchMode = 10003 THEN FPerf ELSE 0 END) AS FDMAmt3, SUM(CASE WHEN FDispatchMode = 10004 THEN FQuantity ELSE 0 END) AS FDMQty4, SUM(CASE WHEN FDispatchMode = 10004 THEN FPerf ELSE 0 END) AS FDMAmt4, SUM(CASE WHEN FWorkType = 11001 THEN FQuantity ELSE 0 END) AS FWorkQty1, SUM(CASE WHEN FWorkType = 11001 THEN FPerf ELSE 0 END) AS FWorkAmt1, SUM(CASE WHEN FWorkType = 11002 THEN FQuantity ELSE 0 END) AS FWorkQty2, SUM(CASE WHEN FWorkType = 11002 THEN FPerf ELSE 0 END) AS FWorkAmt2, SUM(CASE WHEN FWorkType = 11003 THEN FQuantity ELSE 0 END) AS FWorkQty3, SUM(CASE WHEN FWorkType = 11003 THEN FPerf ELSE 0 END) AS FWorkAmt3, SUM(CASE WHEN FWorkType = 11004 THEN FQuantity ELSE 0 END) AS FWorkQty4, SUM(CASE WHEN FWorkType = 11004 THEN FPerf ELSE 0 END) AS FWorkAmt4 FROM ( --可能有一个项目两种支付方式的情况,数量只能算一个 SELECT FEmpId, FBillDetailId, FItemTypeId, FQuantity,FDispatchMode,FWorkType, SUM(FPerf) AS FPerf FROM #EmpPerfDetail WHERE FCategory = 'Item' GROUP BY FEmpId, FBillDetailId, FItemTypeId, FQuantity,FDispatchMode,FWorkType ) AS A GROUP BY A.FEmpId,A.FItemTypeId ) AS A GROUP BY A.FEmpId,A.FItemTypeId,A.FCagegory --统计一级分类(大类) INSERT #ItemDetail(FEmpId,FCagegory,FItemTypeId, FTotalAmt, FTotalQty, FCashAmt, FCardAmt, FOtherAmt, FCashQty, FCardQty, FOtherQty, FDMQty1, FDMAmt1, FDMQty2, FDMAmt2, FDMQty3,FDMAmt3,FDMQty4,FDMAmt4, FWorkQty1, FWorkAmt1, FWorkQty2, FWorkAmt2, FWorkQty3,FWorkAmt3,FWorkQty4,FWorkAmt4) SELECT A.FEmpId, 'ItemKind', B.FParentId, SUM(FTotalAmt), SUM(FTotalQty), SUM(FCashAmt), SUM(FCardAmt), SUM(FOtherAmt), SUM(FCashQty), SUM(FCardQty), SUM(FOtherQty), SUM(FDMQty1), SUM(FDMAmt1), SUM(FDMQty2), SUM(FDMAmt2), SUM(FDMQty3),SUM(FDMAmt3),SUM(FDMQty4),SUM(FDMAmt4), SUM(FWorkQty1), SUM(FWorkAmt1), SUM(FWorkQty2), SUM(FWorkAmt2), SUM(FWorkQty3),SUM(FWorkAmt3),SUM(FWorkQty4),SUM(FWorkAmt4) FROM #ItemDetail AS A,#ItemType AS B WHERE A.FItemTypeId = B.FTypeId GROUP BY A.FEmpId,B.FParentId --把属于大类从前面的小类中删除掉 --提成 INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT FEmpId,'CommTotal',0 AS FQty, SUM(FComm) FROM #EmpPerfDetail GROUP BY FEmpId INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT FEmpId,'ItemComm',0 AS FQty, SUM(FComm) FROM #EmpPerfDetail WHERE FCategory = 'Item' GROUP BY FEmpId INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT FEmpId,'GoodsComm',0 AS FQty, SUM(FComm) FROM #EmpPerfDetail WHERE FCategory = 'Goods' GROUP BY FEmpId INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT FEmpId,'CardComm',0 AS FQty, SUM(FComm) FROM #EmpPerfDetail WHERE FCategory IN ('CardSale','CardRchg') GROUP BY FEmpId INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT FEmpId,'CourseComm',0 AS FQty, SUM(FComm) FROM #EmpPerfDetail WHERE FCategory IN ('CourseSale','CoursePackSale') GROUP BY FEmpId --所有的员工合计 --Total 表合计 INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT 0 AS FEmpId,FType, SUM(FQty) AS FQty, SUM(FAmt) AS FAmt FROM #Total GROUP BY FType --ItemDetail 表合计 INSERT #ItemDetail(FEmpId,FItemTypeId, FTotalAmt, FTotalQty, FCashAmt, FCardAmt, FOtherAmt, FCashQty, FCardQty, FOtherQty, FDMQty1, FDMAmt1, FDMQty2, FDMAmt2, FDMQty3,FDMAmt3,FDMQty4,FDMAmt4, FWorkQty1, FWorkAmt1, FWorkQty2, FWorkAmt2, FWorkQty3,FWorkAmt3,FWorkQty4,FWorkAmt4) SELECT 0, A.FItemTypeId, SUM(FTotalAmt), SUM(FTotalQty), SUM(FCashAmt), SUM(FCardAmt), SUM(FOtherAmt), SUM(FCashQty), SUM(FCardQty), SUM(FOtherQty), SUM(FDMQty1), SUM(FDMAmt1), SUM(FDMQty2), SUM(FDMAmt2), SUM(FDMQty3),SUM(FDMAmt3),SUM(FDMQty4),SUM(FDMAmt4), SUM(FWorkQty1), SUM(FWorkAmt1), SUM(FWorkQty2), SUM(FWorkAmt2), SUM(FWorkQty3),SUM(FWorkAmt3),SUM(FWorkQty4),SUM(FWorkAmt4) FROM #ItemDetail AS A GROUP BY FItemTypeId SELECT FEmpId AS EmpId,FEmpNo AS EmpNo,FEmpName AS EmpName FROM #Employee AS B WHERE EXISTS(SELECT 1 FROM #EmpPerfDetail AS A WHERE A.FEmpId = B.FEmpId) ORDER BY FEmpNo --把编号加前缀 SELECT FCategory + FTypeNo AS TypeNo, FTypeName AS TypeName ,FCategory AS Category FROM #ItemType ORDER BY FCategory,FTypeNo SELECT A.FEmpId AS EmpId, FType AS Type, FQty AS Qty , FAmt AS Amt FROM #Total AS A ,#Employee AS E WHERE A.FEmpId = E.FEmpId ORDER BY E.FEmpNo SELECT A.FEmpId AS EmpId, --员工ID A.FCagegory + B.FTypeNo AS ItemTypeNo, --项目类别编号 编号加前缀 A.FCagegory AS Category, FTotalAmt AS TotalAmt, --总业绩 FTotalQty AS TotalQty, --总数量 FCashAmt AS CashAmt, --现金业绩 FCardAmt AS CardAmt, --卡付业绩 FOtherAmt AS OtherAmt, --其他业绩 FCashQty AS CashQty, --现金数 FCardQty AS CardQty, --卡付数 FOtherQty AS OtherQty, --其他数 FDMQty1 AS DMQty1, --派遣方式1数量 FDMAmt1 AS DMAmt1, --方式1 业绩 FDMQty2 AS DMQty2, -- FDMAmt2 AS DMAmt2, FDMQty3 AS DMQty3, FDMAmt3 AS DMAmt3, FDMQty4 AS DMQty4, FDMAmt4 AS DMAmt4, FWorkQty1 AS WorkQty1, --岗位1数量 FWorkAmt1 AS WorkAmt1, --岗位1业绩 FWorkQty2 AS WorkQty2, -- FWorkAmt2 AS WorkAmt2, FWorkQty3 AS WorkQty3, FWorkAmt3 AS WorkAmt3, FWorkQty4 AS WorkQty4, FWorkAmt4 AS WorkAmt4 FROM #ItemDetail AS A, #ItemType AS B ,#Employee AS E WHERE B.FTypeId = A.FItemTypeId AND A.FEmpId = E.FEmpId ORDER BY E.FEmpNo,B.FTypeNo END GO IF object_id('dbo.PRptWageReport03','P') IS NOT NULL BEGIN DROP PROCEDURE dbo.PRptWageReport03 END GO CREATE PROCEDURE PRptWageReport03 ( @IParam VARCHAR(8000) ) AS BEGIN /* EXEC dbo.PDropTempTables DECLARE @IParam VARCHAR(8000) SET @IParam='FromDate="2014-01-17"ToDate="2015-01-17"FromEmpNo=""ToEmpNo=""FromItemTypeNo=""ToItemTypeNo=""CompNo="A001"IncChild="1"' */ DECLARE @CompId BIGINT DECLARE @FromDate CHAR(10) DECLARE @ToDate CHAR(10) DECLARE @EmployeeText VARCHAR(30) DECLARE @IncChild CHAR DECLARE @CashierId BIGINT DECLARE @DeptId BIGINT DECLARE @ShowAmt INT --业绩显示为营业额 DECLARE @DeptNo VARCHAR(30) DECLARE @LoadDetail BIT --显示明细 SELECT @CompId = dbo.FNGetParamCompId(@IParam) SELECT @FromDate = CONVERT(CHAR(10),dbo.FNGetParamValue(@IParam,'FromDate'),23) SELECT @ToDate = CONVERT(CHAR(10),dbo.FNGetParamValue(@IParam,'ToDate'),23) SELECT @EmployeeText = ISNULL(dbo.FNGetParamValue(@IParam,'Employees'),'') SELECT @ShowAmt = ISNULL(dbo.FNGetParamValue(@IParam,'ShowAmt'),0) SELECT @DeptNo = ISNULL(dbo.FNGetParamValue(@IParam,'DeptNo'),'') SELECT @LoadDetail = ISNULL(dbo.FNGetParamValue(@IParam,'LoadDetail'),0) IF ISNULL(@CompId,0) = 0 OR ISNULL(@ToDate,'') = '' OR ISNULL(@FromDate,'') = '' OR @FromDate > @ToDate BEGIN RETURN END IF @DeptNo = '' OR @DeptNo = '*' BEGIN SET @DeptId = 0 END SELECT @DeptId = FId FROM TBasicType WITH(NOLOCK) WHERE FKey = 'Department' AND FNo = @DeptNo DECLARE @Employees TABLE ( FNo VARCHAR(30) ) CREATE TABLE #PerfTotal ( FEmpId BIGINT NOT NULL, --员工ID FEmpNo VARCHAR(30) NOT NULL, --工号 FEmpName VARCHAR(50) NOT NULL, --姓名 FCustQty FLOAT NOT NULL DEFAULT 0, --总客数 FPerfTotal FLOAT NOT NULL DEFAULT 0, --总业绩 FItemDM1Qty FLOAT NOT NULL DEFAULT 0, --指定客数 FItemDM2Qty FLOAT NOT NULL DEFAULT 0, --轮牌客数 FItemDM3Qty FLOAT NOT NULL DEFAULT 0, --方式3客数 FItemDM4Qty FLOAT NOT NULL DEFAULT 0, --方式4客数 FItemDM1Perf FLOAT NOT NULL DEFAULT 0, --指定业绩 FItemDM2Perf FLOAT NOT NULL DEFAULT 0, --轮牌业绩 FItemDM3Perf FLOAT NOT NULL DEFAULT 0, --方式3业绩 FItemDM4Perf FLOAT NOT NULL DEFAULT 0, --方式4业绩 FWorkType1Perf FLOAT NOT NULL DEFAULT 0, --岗位1业绩 FWorkType2Perf FLOAT NOT NULL DEFAULT 0, --岗位2业绩 FWorkType3Perf FLOAT NOT NULL DEFAULT 0, --岗位3业绩 FWorkType4Perf FLOAT NOT NULL DEFAULT 0, --岗位4业绩 FItemPerfTotal FLOAT NOT NULL DEFAULT 0, --项目总业绩 FItemPerfCashPay FLOAT NOT NULL DEFAULT 0, --项目现金业绩 FItemPerfCardPay FLOAT NOT NULL DEFAULT 0, --项目卡业绩 FItemPerfOtherPay FLOAT NOT NULL DEFAULT 0, --项目其他业绩 FGoodsPerf FLOAT NOT NULL DEFAULT 0, --产品业绩 FCourseSalePerf FLOAT NOT NULL DEFAULT 0, --疗程销售业绩 FCardSalePerf FLOAT NOT NULL DEFAULT 0, --储值卡销售业绩 FMemSalePerf FLOAT NOT NULL DEFAULT 0, --卡销售业绩 FLaborPerf FLOAT NOT NULL DEFAULT 0, --劳动业绩 FCashPerfTotal FLOAT NOT NULL DEFAULT 0, --总现金业绩 FCashAmtTotal FLOAT NOT NULL DEFAULT 0, --总现金营业额 FDM1Rate FLOAT NOT NULL DEFAULT 0, --回头率 FSanction FLOAT NOT NULL DEFAULT 0, --奖罚 FCommTotal FLOAT NOT NULL DEFAULT 0 --总提成 ) INSERT @Employees SELECT Field FROM dbo.FNStrSplit(@EmployeeText) --初始化员工表 --生成员工列表 INSERT #PerfTotal(FEmpId,FEmpNo,FEmpName) SELECT A.FId AS FEmpId,FNo AS FEmpNo ,A.FName AS FEmpName FROM TEmployee AS A WITH(NOLOCK) ,TEmployeeSub AS B WITH(NOLOCK) WHERE A.FId != 0 AND B.FCompId = @CompId AND B.FEmpId = A.FId AND (@DeptId = 0 OR A.FDeptId = @DeptId) AND (@EmployeeText = '' OR A.FNo IN (select FNo from @Employees) ) /* IF @@ROWCOUNT = 0 BEGIN RETURN END */ CREATE TABLE #Company ( FCompId BIGINT, FCompNo VARCHAR(20) ) INSERT #Company (FCompID,FCompNo) VALUES(@CompId,'') DECLARE @Error INT EXEC @Error = PGenerateEmpPerf 0,@FromDate,@ToDate IF @Error != 0 BEGIN RETURN -1 END SELECT FParentId,FChildId,FLevel INTO #Tree FROM TBasicTypeTree WHERE FKey = 'ItemType' ORDER BY FLevel DESC --保证最后更新的是最深级别 --把所有符合条件的数据拿到临时表中 #EmpPerfDetail SELECT A.*,CAST('' AS VARCHAR(20)) AS FPayTypeCatg /*支付大类(现金,卡,其他)*/ INTO #EmpPerfDetail FROM TEmpPerfDetail# AS A, #PerfTotal AS E,#Company AS C WHERE A.FWorkDate BETWEEN @FromDate AND @ToDate AND C.FCompId = A.FCompId AND A.FEmpId = E.FEmpId AND A.FIsExpired = 0 SELECT FEmpId INTO #Employee FROM #PerfTotal --计算动态算法业绩 EXEC PCalcDynamicEmpComm @CompId,0 --显示为营业额 IF @ShowAmt = 1 BEGIN UPDATE #EmpPerfDetail SET FPerf = FAmt END ELSE IF @ShowAmt = 2 BEGIN UPDATE #EmpPerfDetail SET FPerf = FDedCostPerf END --设置支付大类 UPDATE A SET A.FPayTypeCatg = CASE WHEN A.FPayTypeId IN(1,2) THEN 'Cash' --现金 WHEN (A.FPayTypeId IN(6) OR ISNULL(S.FIsAccount,0) = 1) THEN 'Card' --卡付 ELSE 'Other' END --支付大类, 2 现金,1 卡付, 3其他 FROM #EmpPerfDetail AS A,TPayAccount AS S WITH(NOLOCK) WHERE A.FPayTypeId = S.FId --更新项目类别 -- DEBUG --有待改进 /* UPDATE A SET A.FItemTypeId = B.FTypeId FROM #EmpPerfDetail AS A, TItem AS B WITH(NOLOCK) WHERE A.FItemId = B.FId AND A.FCategory = 'Item'*/ --更新项目数量,因为有支付拆分的情况 --所有的收银单据,每个员工一行 SELECT A.FEmpId AS FEmpId, A.FBillId AS FBillId, MAX(A.FCustQuantity) AS FCustQuantity, CASE WHEN (MIN(A.FCustGender)) = 1 THEN 'ItemMale' ELSE 'ItemFemale' END AS FCustGender, --客户性别 CASE WHEN (MIN(CASE WHEN A.FDispatchMode = 10001 THEN 1 ELSE 2 END)) = 1 THEN 'ItemDMode1' ELSE 'ItemDMode2' END AS FDispatchMode, --1指定,2轮牌 'Item' + MIN(A.FPayTypeCatg) AS FPayCatg --支付大类, 2 现金,1 卡付, 3其他 INTO #ItemBill FROM #EmpPerfDetail AS A LEFT JOIN TPayAccount AS S WITH(NOLOCK) ON S.FId = A.FPayTypeId WHERE FCategory = 'Item' GROUP BY A.FEmpId,A.FBillId --项目汇总 --总客数 UPDATE A SET A.FCustQty = B.FQty FROM #PerfTotal AS A, (SELECT FEmpId, SUM(FCustQuantity) AS FQty FROM #ItemBill GROUP BY FEmpId) AS B WHERE B.FEmpId = A.FEmpId --总项目业绩 UPDATE A SET A.FItemPerfTotal = B.FPerf FROM #PerfTotal AS A, (SELECT FEmpId,SUM(CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ) AS FPerf FROM #EmpPerfDetail WHERE FCategory = 'Item' GROUP BY FEmpId) AS B WHERE B.FEmpId = A.FEmpId --指定客 UPDATE A SET A.FItemDM1Qty = B.FDM1Qty, A.FItemDM2Qty = B.FDM2Qty, A.FItemDM3Qty = 0, A.FItemDM4Qty = 0 FROM #PerfTotal AS A, ( SELECT FEmpId,SUM(CASE FDispatchMode WHEN 'ItemDMode1' THEN FCustQuantity ELSE 0 END) AS FDM1Qty, SUM(CASE FDispatchMode WHEN 'ItemDMode2' THEN FCustQuantity ELSE 0 END) AS FDM2Qty FROM #ItemBill GROUP BY FEmpId ) AS B WHERE A.FEmpId = B.FEmpId --指定业绩 UPDATE A SET A.FItemDM1Perf = B.FDM1Perf, A.FItemDM2Perf = B.FDM2Perf, A.FItemDM3Perf = B.FDM3Perf, A.FItemDM4Perf = B.FDM4Perf FROM #PerfTotal AS A, ( SELECT FEmpId,SUM(CASE FDispatchMode WHEN 10001 THEN CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ELSE 0 END) AS FDM1Perf, SUM(CASE FDispatchMode WHEN 10002 THEN CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ELSE 0 END) AS FDM2Perf, SUM(CASE FDispatchMode WHEN 10003 THEN CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ELSE 0 END) AS FDM3Perf, SUM(CASE FDispatchMode WHEN 10004 THEN CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ELSE 0 END) AS FDM4Perf FROM #EmpPerfDetail GROUP BY FEmpId ) AS B WHERE A.FEmpId = B.FEmpId --工种业绩 UPDATE A SET A.FWorkType1Perf = B.FWorkType1Perf, A.FWorkType2Perf = B.FWorkType2Perf, A.FWorkType3Perf = B.FWorkType3Perf, A.FWorkType4Perf = B.FWorkType4Perf FROM #PerfTotal AS A, ( SELECT FEmpId,SUM(CASE FWorkType WHEN 11001 THEN CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ELSE 0 END) AS FWorkType1Perf, SUM(CASE FWorkType WHEN 11002 THEN CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ELSE 0 END) AS FWorkType2Perf, SUM(CASE FWorkType WHEN 11003 THEN CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ELSE 0 END) AS FWorkType3Perf, SUM(CASE FWorkType WHEN 11004 THEN CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ELSE 0 END) AS FWorkType4Perf FROM #EmpPerfDetail GROUP BY FEmpId ) AS B WHERE A.FEmpId = B.FEmpId --项目现金业绩 UPDATE A SET A.FItemPerfCashPay = B.FCash, A.FItemPerfCardPay = B.FCard, A.FItemPerfOtherPay = B.FOther FROM #PerfTotal AS A, (SELECT FEmpId,SUM(CASE FPayTypeCatg WHEN 'Cash' THEN CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ELSE 0 END) AS FCash, SUM(CASE FPayTypeCatg WHEN 'Card' THEN CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ELSE 0 END) AS FCard, SUM(CASE FPayTypeCatg WHEN 'Other' THEN CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ELSE 0 END) AS FOther FROM #EmpPerfDetail WHERE FCategory = 'Item' GROUP BY FEmpId) AS B WHERE A.FEmpId = B.FEmpId /* --新客老客,暂时没有 UNION ALL --现,卡付,其他支付客数 SELECT FEmpId, FPayCatg AS FType, COUNT(1) AS FQty, 0 AS FAmt FROM #ItemBill GROUP BY FEmpId, FPayCatg UNION ALL --现金,卡,其他支付业绩 SELECT A.FEmpId, 'Item' + A.FPayTypeCatg, 0 AS FQty, SUM(A.FPerf) AS FAmt FROM #EmpPerfDetail AS A WHERE A.FCategory = 'Item' GROUP BY A.FEmpId,A.FPayTypeCatg ) AS A GROUP BY A.FEmpId,A.FType --按项目类别分组汇总 --把属于大类从前面的小类中删除掉 --提成 INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT FEmpId,'CommTotal',0 AS FQty, SUM(FComm) FROM #EmpPerfDetail GROUP BY FEmpId INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT FEmpId,'ItemComm',0 AS FQty, SUM(FComm) FROM #EmpPerfDetail WHERE FCategory = 'Item' GROUP BY FEmpId INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT FEmpId,'GoodsComm',0 AS FQty, SUM(FComm) FROM #EmpPerfDetail WHERE FCategory = 'Goods' GROUP BY FEmpId INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT FEmpId,'CardComm',0 AS FQty, SUM(FComm) FROM #EmpPerfDetail WHERE FCategory IN ('CardSale','CardRchg') GROUP BY FEmpId INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT FEmpId,'CourseComm',0 AS FQty, SUM(FComm) FROM #EmpPerfDetail WHERE FCategory = 'SellCourse' GROUP BY FEmpId --所有的员工合计 --Total 表合计 INSERT #Total(FEmpId,FType,FQty,FAmt) SELECT 0 AS FEmpId,FType, SUM(FQty) AS FQty, SUM(FAmt) AS FAmt FROM #Total GROUP BY FType SELECT FEmpId AS EmpId,FEmpNo AS EmpNo,FEmpName AS EmpName FROM #Employee ORDER BY FEmpNo SELECT A.FEmpId AS EmpId, FType AS Type, FQty AS Qty , FAmt AS Amt FROM #Total AS A ,#Employee AS E WHERE A.FEmpId = E.FEmpId ORDER BY E.FEmpNo */ --产品总业绩 UPDATE A SET A.FGoodsPerf = B.FPerf FROM #PerfTotal AS A, (SELECT FEmpId, SUM(CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ) AS FPerf FROM #EmpPerfDetail WHERE FCategory = 'Goods' GROUP BY FEmpId ) AS B WHERE A.FEmpId = B.FEmpId --储值卡销售 UPDATE A SET A.FCardSalePerf = B.FPerf FROM #PerfTotal AS A,(SELECT FEmpId,SUM(FPerf) AS FPerf FROM #EmpPerfDetail WHERE FCategory IN('CardSale','CardRchg','','') GROUP BY FEmpId) AS B WHERE A.FEmpId = B.FEmpId --疗程销售 UPDATE A SET A.FCourseSalePerf = B.FPerf FROM #PerfTotal AS A,(SELECT FEmpId,SUM(FPerf) AS FPerf FROM #EmpPerfDetail WHERE FCategory IN('','','CourseSale','CoursePackSale') GROUP BY FEmpId) AS B WHERE A.FEmpId = B.FEmpId --总卡销售 UPDATE A SET A.FMemSalePerf = A.FCardSalePerf + A.FCourseSalePerf FROM #PerfTotal AS A --劳动业绩 = 产品+项目,总业绩= 卡,产品,项目之和,回头率=指定业绩/总业绩 DECLARE @Value VARCHAR(5) EXEC PGetBizParameter @CompId,'EmpLaborPerfIncludeGoods',@Value OUTPUT UPDATE A SET A.FPerfTotal = A.FGoodsPerf + A.FItemPerfTotal + A.FMemSalePerf, A.FLaborPerf = CASE WHEN ISNULL(@Value,'True') = 'True' THEN A.FGoodsPerf ELSE 0 END + A.FItemPerfTotal, --劳动业绩不包含产品 A.FDM1Rate = CASE WHEN A.FItemPerfTotal = 0 THEN 0 ELSE A.FItemDM1Perf / A.FItemPerfTotal END FROM #PerfTotal AS A --总现金营业额 UPDATE A SET A.FCashAmtTotal = B.FAmt, A.FCashPerfTotal = B.FPerf FROM #PerfTotal AS A,(SELECT FEmpId,SUM(FAmt) AS FAmt,SUM(CASE WHEN @ShowAmt = 1 THEN FAmt ELSE FPerf END ) AS FPerf FROM #EmpPerfDetail WHERE FPayTypeId IN(0,1,2) GROUP BY FEmpId) AS B WHERE A.FEmpId = B.FEmpId --总提成 UPDATE A SET A.FCommTotal = B.FComm FROM #PerfTotal AS A,(SELECT FEmpId,SUM(FComm) AS FComm FROM #EmpPerfDetail GROUP BY FEmpId) AS B WHERE A.FEmpId = B.FEmpId SELECT FEmpNo + '-' + FEmpName AS EmpInfo, FEmpNo AS EmpNo, FItemPerfTotal AS ItemPerfTotal, --项目总业绩 FCustQty AS CustQty, --总客数 FPerfTotal AS PerfTotal, --总业绩 FItemDM1Qty AS ItemDM1Qty, --指定客数 FItemDM2Qty AS ItemDM2Qty, --轮牌客数 FItemDM3Qty AS ItemDM3Qty, --方式3客数 FItemDM4Qty AS ItemDM4Qty, --方式4客数 FItemDM1Perf AS ItemDM1Perf, --指定业绩 FItemDM2Perf AS ItemDM2Perf, --轮牌业绩 FItemDM3Perf AS ItemDM3Perf, --方式3业绩 FItemDM4Perf AS ItemDM4Perf, --方式4业绩 FWorkType1Perf AS WorkType1Perf, --岗位1业绩 FWorkType2Perf AS WorkType2Perf, --岗位2业绩 FWorkType3Perf AS WorkType3Perf, --岗位3业绩 FWorkType4Perf AS WorkType4Perf, --岗位4业绩 FGoodsPerf AS GoodsPerf, --产品业绩 FCourseSalePerf AS CourseSalePerf, --疗程销售业绩 FCardSalePerf AS CardSalePerf, --储值卡销售业绩 FMemSalePerf AS MemSalePerf, --卡销售业绩 FLaborPerf AS LaborPerf, --劳动业绩 FItemPerfCashPay AS ItemPerfCashPay, --项目现金业绩 FItemPerfCardPay AS ItemPerfCardPay, --项目卡业绩 FItemPerfOtherPay AS ItemPerfOtherPay, --项目其他业绩 FCashPerfTotal AS CashPerfTotal, --总现金业绩 FCashAmtTotal AS CashAmtTotal, --总现金营业额 FDM1Rate AS DM1Rate, --回头率 FSanction AS Sanction, --奖罚 FCommTotal AS CommTotal --总提成 FROM #PerfTotal WHERE FCustQty != 0 OR FPerfTotal != 0 OR FCommTotal != 0 OR FCashPerfTotal != 0 OR FMemSalePerf != 0 OR FGoodsPerf != 0 ORDER BY FEmpNo SELECT '' AS EmpInfo, SUM(FItemPerfTotal) AS ItemPerfTotal, --项目总业绩 SUM(FCustQty) AS CustQty, --总客数 SUM(FPerfTotal) AS PerfTotal, --总业绩 SUM(FItemDM1Qty) AS ItemDM1Qty, --指定客数 SUM(FItemDM2Qty) AS ItemDM2Qty, --轮牌客数 SUM(FItemDM3Qty) AS ItemDM3Qty, --方式3客数 SUM(FItemDM4Qty) AS ItemDM4Qty, --方式4客数 SUM(FItemDM1Perf) AS ItemDM1Perf, --指定业绩 SUM(FItemDM2Perf) AS ItemDM2Perf, --轮牌业绩 SUM(FItemDM3Perf) AS ItemDM3Perf, --方式3业绩 SUM(FItemDM4Perf) AS ItemDM4Perf, --方式4业绩 SUM(FWorkType1Perf) AS WorkType1Perf, --岗位1业绩 SUM(FWorkType2Perf) AS WorkType2Perf, --岗位2业绩 SUM(FWorkType3Perf) AS WorkType3Perf, --岗位3业绩 SUM(FWorkType4Perf) AS WorkType4Perf, --岗位4业绩 SUM(FGoodsPerf) AS GoodsPerf, --产品业绩 SUM(FCourseSalePerf) AS CourseSalePerf, --疗程销售业绩 SUM(FCardSalePerf) AS CardSalePerf, --储值卡销售业绩 SUM(FMemSalePerf) AS MemSalePerf, --卡销售业绩 SUM(FLaborPerf) AS LaborPerf, --劳动业绩 SUM(FItemPerfCashPay) AS ItemPerfCashPay, --项目现金业绩 SUM(FItemPerfCardPay) AS ItemPerfCardPay, --项目卡业绩 SUM(FItemPerfOtherPay) AS ItemPerfOtherPay, --项目其他业绩 SUM(FCashPerfTotal) AS CashPerfTotal, --总现金业绩 SUM(FCashAmtTotal) AS CashAmtTotal, --总现金营业额 SUM(FDM1Rate) AS DM1Rate, --回头率 SUM(FSanction) AS Sanction, --奖罚 SUM(FCommTotal) AS CommTotal --总提成 FROM #PerfTotal WHERE FCustQty != 0 OR FPerfTotal != 0 OR FCommTotal != 0 OR FCashPerfTotal != 0 OR FMemSalePerf != 0 OR FGoodsPerf != 0 END GO IF object_id('dbo.PRptPadWageReport04','P') IS NOT NULL BEGIN DROP PROCEDURE dbo.PRptPadWageReport04 END GO CREATE PROCEDURE dbo.PRptPadWageReport04 ( @IParam VARCHAR(8000) ) AS BEGIN DECLARE @CompId BIGINT DECLARE @FromDate VARCHAR(20) DECLARE @ToDate VARCHAR(20) DECLARE @EmpNo VARCHAR(20) DECLARE @Version VARCHAR(20) SELECT @CompId = dbo.FNGetParamCompId(@IParam) SELECT @FromDate = dbo.FNGetParamValue(@IParam,'FromDate') SELECT @ToDate = dbo.FNGetParamValue(@IParam,'ToDate') IF ISDATE(@FromDate) != 1 OR ISDATE(@ToDate) != 1 BEGIN RETURN END SELECT @FromDate = CONVERT(varchar(100),CAST(@FromDate AS DATETIME), 23) SELECT @ToDate = CONVERT(varchar(100), CAST(@ToDate AS DATETIME), 23) SELECT @EmpNo = dbo.FNGetParamValue(@IParam,'EmpNo') SELECT @Version = dbo.FNGetParamValue(@IParam,'Version') IF @CompId = 0 OR @FromDate IS NULL OR @ToDate IS NULL BEGIN RETURN END DECLARE @AllCompany BIT SELECT @AllCompany = 0 CREATE TABLE #Company ( FCompId BIGINT ) DECLARE @EmpId BIGINT SELECT @EmpId = FId FROM TEmployee WITH(NOLOCK) WHERE FNo = @EmpNo INSERT #Company SELECT @CompId EXEC PGenerateEmpPerf @AllCompany,@FromDate,@ToDate --把所有符合条件的数据拿到临时表中 #PerfDetail SELECT A.* INTO #EmpPerfDetail FROM TEmpPerfDetail# AS A,#Company AS C WHERE A.FWorkDate BETWEEN @FromDate AND @ToDate AND C.FCompId = A.FCompId AND A.FEmpId = @EmpId AND A.FIsExpired = 0 --计算动态算法业绩 EXEC PCalcDynamicEmpComm @CompId,@EmpId --所有的收银单据,每个员工一行 SELECT A.FBillId AS FBillId, CASE WHEN (MIN(A.FCustGender)) = 1 THEN 'ItemMale' ELSE 'ItemFemale' END AS FCustGender, --客户性别 CASE WHEN (MIN(CASE WHEN A.FDispatchMode = 10001 THEN 1 ELSE 2 END)) = 1 THEN 'ItemDMode1' ELSE 'ItemDMode2' END AS FDispatchMode--, --1指定,2轮牌 -- 'Item' + MIN(A.FPayTypeCatg) AS FPayCatg --支付大类, 2 现金,1 卡付, 3其他 INTO #ItemBill FROM #EmpPerfDetail AS A LEFT JOIN TPayAccount AS S WITH(NOLOCK) ON S.FId = A.FPayTypeId WHERE FCategory = 'Item' GROUP BY A.FBillId IF ISNULL(@Version,'') = '1' BEGIN CREATE TABLE #Total ( Category VARCHAR(20), Quantity DECIMAL(18,6), PayAmt DECIMAL(18,6), Perf DECIMAL(18,6), Comm DECIMAL(18,6) ) --总客数 INSERT #Total(Category,Quantity,PayAmt,Perf,Comm) SELECT '' AS FCagegory, COUNT(1) AS FQty, 0 AS FAmt ,0 AS Perf,0 AS Comm FROM #ItemBill --劳动业绩 UPDATE A SET A.Perf = B.FPerf FROM #Total AS A, (SELECT SUM(FPerf) AS FPerf FROM EmpPerfDetail WHERE FCategory IN( 'Item','Goods')) AS B WHERE Category = '' --每种类别汇总 INSERT #Total(Category,Quantity,PayAmt,Perf,Comm) SELECT FCategory AS FCagegory, ROUND(SUM(FSplitQuantity),2), ROUND(SUM(FAmt),2) AS FPayAmt, ROUND(SUM(FPerf),2) AS FPerf,ROUND(SUM(FComm),2) AS FComm FROM #PerfDetail GROUP BY FCategory INSERT #Total(Category,Quantity,PayAmt,Perf,Comm) SELECT 'Summary',SUM(Quantity),SUM(PayAmt),SUM(Perf),SUM(Comm) FROM #Total WHERE Category != '' SELECT * FROM #Total SELECT A.FCategory AS Category, A.FBillNo AS BillNo, A.FSplitQuantity AS Quantity, A.FAmt AS PayAmt,S.FName AS PayTypeName ,FDispatchMode AS DispatchMode, A.FPerf AS Perf ,A.FComm AS Comm,A.FItemName AS ItemName ,REPLACE( CONVERT(varchar(100), FBillTime, 20),' ',CHAR(13) + CHAR(10)) AS BillTime FROM #PerfDetail AS A LEFT JOIN TPayAccount AS S ON S.FId = A.FPayTypeId ORDER BY A.FCategory , A.FBillTime END ELSE BEGIN SELECT A.FEmpId AS EmpId, A.FCategory AS Category, A.FBillNo AS BillNo, A.FQuantity AS Quantity, A.FAmt AS Amt, A.FPerf AS Perf ,A.FComm AS Comm,A.FItemName AS ItemName ,REPLACE( CONVERT(varchar(100), FBillTime, 20),' ',CHAR(10) + CHAR(13)) AS BillDate FROM #PerfDetail AS A ORDER BY A.FCategory , A.FBillTime END END GO IF object_id('dbo.PRptPadWageReport04','P') IS NOT NULL BEGIN DROP PROCEDURE dbo.PRptPadWageReport04 END GO CREATE PROCEDURE dbo.PRptPadWageReport04 ( @IParam VARCHAR(8000) ) AS BEGIN DECLARE @CompId BIGINT DECLARE @FromDate VARCHAR(20) DECLARE @ToDate VARCHAR(20) DECLARE @EmpNo VARCHAR(20) DECLARE @Version VARCHAR(20) SELECT @CompId = dbo.FNGetParamCompId(@IParam) SELECT @FromDate = dbo.FNGetParamValue(@IParam,'FromDate') SELECT @ToDate = dbo.FNGetParamValue(@IParam,'ToDate') IF ISDATE(@FromDate) != 1 OR ISDATE(@ToDate) != 1 BEGIN RETURN END SELECT @FromDate = CONVERT(varchar(100),CAST(@FromDate AS DATETIME), 23) SELECT @ToDate = CONVERT(varchar(100), CAST(@ToDate AS DATETIME), 23) SELECT @EmpNo = dbo.FNGetParamValue(@IParam,'EmpNo') SELECT @Version = dbo.FNGetParamValue(@IParam,'Version') IF @CompId = 0 OR @FromDate IS NULL OR @ToDate IS NULL BEGIN RETURN END DECLARE @AllCompany BIT SELECT @AllCompany = 0 CREATE TABLE #Company ( FCompId BIGINT ) DECLARE @EmpId BIGINT SELECT @EmpId = FId FROM TEmployee WITH(NOLOCK) WHERE FNo = @EmpNo INSERT #Company SELECT @CompId EXEC PGenerateEmpPerf @AllCompany,@FromDate,@ToDate --把所有符合条件的数据拿到临时表中 #EmpPerfDetail SELECT A.* INTO #EmpPerfDetail FROM TEmpPerfDetail# AS A,#Company AS C WHERE A.FWorkDate BETWEEN @FromDate AND @ToDate AND C.FCompId = A.FCompId AND A.FEmpId = @EmpId AND A.FIsExpired = 0 --计算动态业绩 EXEC PCalcDynamicEmpComm @CompId,@EmpId --所有的收银单据,每个员工一行 SELECT A.FBillId AS FBillId, CASE WHEN (MIN(A.FCustGender)) = 1 THEN 'ItemMale' ELSE 'ItemFemale' END AS FCustGender, --客户性别 CASE WHEN (MIN(CASE WHEN A.FDispatchMode = 10001 THEN 1 ELSE 2 END)) = 1 THEN 'ItemDMode1' ELSE 'ItemDMode2' END AS FDispatchMode--, --1指定,2轮牌 -- 'Item' + MIN(A.FPayTypeCatg) AS FPayCatg --支付大类, 2 现金,1 卡付, 3其他 INTO #ItemBill FROM #EmpPerfDetail AS A LEFT JOIN TPayAccount AS S WITH(NOLOCK) ON S.FId = A.FPayTypeId WHERE FCategory = 'Item' GROUP BY A.FBillId IF ISNULL(@Version,'') = '1' BEGIN CREATE TABLE #Total ( Category VARCHAR(20), Quantity DECIMAL(18,6), PayAmt DECIMAL(18,6), Perf DECIMAL(18,6), Comm DECIMAL(18,6) ) --总客数 INSERT #Total(Category,Quantity,PayAmt,Perf,Comm) SELECT '' AS FCagegory, COUNT(1) AS FQty, 0 AS FAmt ,0 AS Perf,0 AS Comm FROM #ItemBill --劳动业绩 UPDATE A SET A.Perf = B.FPerf FROM #Total AS A, (SELECT SUM(FPerf) AS FPerf FROM #EmpPerfDetail WHERE FCategory IN( 'Item','Goods')) AS B WHERE Category = '' --每种类别汇总 INSERT #Total(Category,Quantity,PayAmt,Perf,Comm) SELECT FCategory AS FCagegory, ROUND(SUM(FSplitQuantity),2), ROUND(SUM(FAmt),2) AS FPayAmt, ROUND(SUM(FPerf),2) AS FPerf,ROUND(SUM(FComm),2) AS FComm FROM #EmpPerfDetail GROUP BY FCategory INSERT #Total(Category,Quantity,PayAmt,Perf,Comm) SELECT 'Summary',SUM(Quantity),SUM(PayAmt),SUM(Perf),SUM(Comm) FROM #Total WHERE Category != '' SELECT * FROM #Total SELECT A.FCategory AS Category, A.FBillNo AS BillNo, A.FSplitQuantity AS Quantity, A.FAmt AS PayAmt,S.FName AS PayTypeName ,FDispatchMode AS DispatchMode, A.FPerf AS Perf ,A.FComm AS Comm,A.FItemName AS ItemName ,REPLACE( CONVERT(varchar(100), FBillTime, 20),' ',CHAR(13) + CHAR(10)) AS BillTime FROM #EmpPerfDetail AS A LEFT JOIN TPayAccount AS S ON S.FId = A.FPayTypeId ORDER BY A.FCategory , A.FBillTime END ELSE BEGIN SELECT A.FEmpId AS EmpId, A.FCategory AS Category, A.FBillNo AS BillNo, A.FQuantity AS Quantity, A.FAmt AS Amt, A.FPerf AS Perf ,A.FComm AS Comm,A.FItemName AS ItemName ,REPLACE( CONVERT(varchar(100), FBillTime, 20),' ',CHAR(10) + CHAR(13)) AS BillDate FROM #EmpPerfDetail AS A ORDER BY A.FCategory , A.FBillTime END END GO IF object_id('dbo.PRptWageReport01','P') IS NOT NULL BEGIN DROP PROCEDURE dbo.PRptWageReport01 END GO CREATE PROCEDURE PRptWageReport01 ( @IParam VARCHAR(8000) ) AS BEGIN DECLARE @CompanyStr VARCHAR(8000) DECLARE @FromDate CHAR(10) DECLARE @ToDate CHAR(10) DECLARE @IncChild CHAR DECLARE @CashierId BIGINT DECLARE @DeptId BIGINT DECLARE @AllCompany BIT DECLARE @PayTypeNo VARCHAR(1000) DECLARE @ItemNo VARCHAR(1000) DECLARE @DeptNo VARCHAR(1000) DECLARE @CompId BIGINT SELECT @CompanyStr = dbo.FNGetParamValue(@IParam,'Company') SELECT @FromDate = CONVERT(CHAR(10),dbo.FNGetParamValue(@IParam,'FromDate'),23) SELECT @ToDate = CONVERT(CHAR(10),dbo.FNGetParamValue(@IParam,'ToDate'),23) SELECT @PayTypeNo = ISNULL(dbo.FNGetParamValue(@IParam,'PayTypeNo'),'') SELECT @ItemNo = ISNULL(dbo.FNGetParamValue(@IParam,'ItemNo'),'') SELECT @DeptNo = ISNULL(dbo.FNGetParamValue(@IParam,'DeptNo'),'') IF ISNULL(@ToDate,'') = '' OR ISNULL(@FromDate,'') = '' OR @FromDate > @ToDate BEGIN RETURN END CREATE TABLE #Company ( FCompId BIGINT, FCompNo VARCHAR(20) ) INSERT #Company SELECT A.FId,A.FNo FROM TCompany AS A WITH(NOLOCK), dbo.FNStrSplit(@CompanyStr) AS B WHERE A.FNo = B.Field SELECT @CompId = FCompId FROM #Company IF @DeptNo = '' OR @DeptNo = '*' BEGIN SET @DeptId = 0 END SELECT @DeptId = FId FROM TBasicType WITH(NOLOCK) WHERE FKey = 'Department' AND FNo = @DeptNo DECLARE @PayTypeIds TABLE ( FPayTypeId BIGINT ) DECLARE @ItemIds TABLE ( FItemId BIGINT ) DECLARE @CourseIds TABLE ( FCourseId BIGINT ) IF @PayTypeNo != '' BEGIN INSERT @PayTypeIds(FPayTypeId) SELECT A.FId FROM TPayAccount AS A WITH(NOLOCK), dbo.FNStrSplit(@PayTypeNo) AS B WHERE A.FNo = B.Field END IF @ItemNo != '' BEGIN INSERT @ItemIds(FItemId) SELECT A.FId FROM TItem AS A WITH(NOLOCK), dbo.FNStrSplit(@ItemNo) AS B WHERE A.FNo = B.Field INSERT @CourseIds(FCourseId) SELECT A.FId FROM TCourse AS A WITH(NOLOCK) ,@ItemIds AS B WHERE A.FItemId = B.FItemId END SET @AllCompany = 0 --生成#Company 中的所有员工业绩 EXEC PGenerateEmpPerf @AllCompany,@FromDate,@ToDate /* SELECT A.FId AS FEmpId , A.FNo FEmpNo, A.FName AS FEmpName, CAST(A.FPosId AS VARCHAR(20)) AS FPosId FROM TEmployee AS A WITH(NOLOCK) INNER JOIN #Company AS C on c.FCompId = a.FCompId UNION ALL */ SELECT A.FId AS FEmpId, A.FNo AS FEmpNo,A.FName AS FEmpName, CAST(A.FPosId AS VARCHAR(20)) AS PosId INTO #Employee FROM TEmployee AS A WITH(NOLOCK) INNER JOIN TEmployeeSub AS B ON B.FEmpId = A.FId INNER JOIN #Company AS C ON C.FCompId = B.FCompId WHERE (@DeptId = 0 OR A.FDeptId = @DeptId) --缓存到临时表 SELECT A.* INTO #EmpPerfDetail FROM TEmpPerfDetail# AS A WITH(NOLOCK),#Employee AS E WHERE A.FIsExpired = 0 AND A.FEmpId = E.FEmpId AND A.FCompId = @CompId AND A.FWorkDate BETWEEN @FromDate AND @ToDate --计算动态提成 EXEC PCalcDynamicEmpComm @CompId,0 SELECT E.FEmpId AS EmpId, E.FEmpNo AS EmpNo, A.FCategory AS Category, FBillNo AS BillNo,FBillDetailId AS BillDetailId, CONVERT(varchar(20),A.FBillTime, 120) AS BillTime,FWorkDate AS WorkDate, FWorkType AS WorkType,FDispatchMode AS DispatchMode, S.FName AS DispatchModeName, FItemNo AS ItemNo, FItemNo + '-' + FItemName AS ItemName, FQuantity AS Quantity, P.FName AS PayTypeName, A.FPayTypeId AS PayTypeId, FAmt AS PayAmt, FPerf AS Perf,FComm AS Comm,FDedCostPerf AS DedCostPerf, CASE WHEN A.FCategory = 'Item' THEN S2.FName WHEN A.FCategory = 'Goods' THEN S4.FName ELSE S3.FName END AS WorkTypeName, A.FMemNo AS MemNo,ISNULL(A.FCustName,M.FName) AS CustName, A.FCardNo AS CardNo, CASE WHEN ISNULL(FCustGender,M.FGender) = 1 THEN '男' ELSE '女' END AS CustGender, CT.FName AS CardTypeName INTO #PerfDetail FROM #Employee AS E, #EmpPerfDetail AS A LEFT JOIN TBasicType AS S WITH(NOLOCK) ON S.FKey = 'ServeDispatchMode' AND S.FId = A.FDispatchMode LEFT JOIN TBasicType AS S2 WITH(NOLOCK) ON S2.FKey = 'ServeWorkType' AND S2.FId = A.FWorkType AND A.FCategory = 'Item' LEFT JOIN TBasicType AS S3 WITH(NOLOCK) ON S3.FKey = 'MemSaleWorkType' AND S3.FId = A.FWorkType AND A.FCategory IN('CardSale','CardRchg','CourseSale','CoursePackSale') LEFT JOIN TBasicType AS S4 WITH(NOLOCK) ON S4.FKey = 'GoodsSaleWorkType' AND S4.FId = A.FWorkType AND A.FCategory IN('Goods') LEFT JOIN TMember AS M WITH(NOLOCK) ON A.FMemId = M.FMemId LEFT JOIN TPayAccount AS P ON P.FId = A.FPayTypeId LEFT JOIN TMemberCardType AS CT ON CT.FId = A.FCardTypeId WHERE E.FEmpId = A.FEmpId AND (@PayTypeNo = '' OR A.FPayTypeId IN(SELECT FPayTypeId FROM @PayTypeIds)) AND (@ItemNo = '' OR (A.FCategory = 'Item' AND A.FItemId IN(SELECT FItemId FROM @ItemIds) ) OR (A.FCategory = 'CourseSale' AND A.FItemId IN(SELECT FCourseId FROM @CourseIds) )) ORDER BY A.FEmpId,CASE WHEN A.FCategory IN('CardSale','CardRchg') THEN 1 WHEN A.FCategory IN( 'CourseSale','CoursePackSale') THEN 2 WHEN A.FCategory = 'Goods' THEN 3 WHEN A.FCategory = 'Item' THEN 4 ELSE 5 END ,FBillTime, FBillNo,FBillDetailId,FWorkType SELECT FEmpNo AS EmpNo, FEmpName AS EmpName FROM #Employee AS B WHERE EXISTS(SELECT TOP 1 1 FROM #PerfDetail AS A WHERE A.EmpId = B.FEmpId) ORDER BY FEmpNo SELECT * FROM #PerfDetail --汇总 SELECT A.EmpId, A.Category ,SumType = CAST('Pay' AS VARCHAR(10)),PayTypeId, SUM(A.PayAmt) AS PayAmt,SUM(DedCostPerf) AS DedCostPerf,SUM(A.Perf) AS Perf,SUM(A.Comm) AS Comm ,CAST(0 AS DECIMAL(18,6)) AS Quantity INTO #Summary FROM #PerfDetail AS A,#Employee AS E--,#Company AS C WHERE A.EmpId = E.FEmpId -- AND A.CompId = C.FCompId AND A.WorkDate BETWEEN @FromDate AND @ToDate -- AND A.FIsExpired = 0 -- AND (@PayTypeNo = '' OR A.FPayTypeId IN(SELECT FPayTypeId FROM @PayTypeIds)) -- AND (@ItemNo = '' OR (A.FCategory = 'Item' AND A.FItemId IN(SELECT FItemId FROM @ItemIds) )) GROUP BY A.EmpId, A.Category ,A.PayTypeId INSERT #Summary(EmpId,Category,SumType,PayAmt,DedCostPerf,Perf,Comm) SELECT EmpId,Category, 'Total',SUM(PayAmt),SUM(DedCostPerf),SUM(Perf),SUM(Comm) FROM #Summary GROUP BY EmpId,Category INSERT #Summary(EmpId,Category,SumType,PayAmt,DedCostPerf,Perf,Comm) SELECT EmpId,'All', 'Total',SUM(PayAmt),SUM(DedCostPerf),SUM(Perf),SUM(Comm) FROM #Summary WHERE SumType = 'Total' GROUP BY EmpId UPDATE A SET A.Quantity = T2.Quantity FROM #Summary AS A, ( SELECT EmpId,Category,SUM(Quantity) AS Quantity FROM ( SELECT A.EmpId,Category,1 AS Quantity FROM #PerfDetail AS A,#Employee AS E,#Company AS C WHERE A.EmpId = E.FEmpId GROUP BY A.EmpId,A.Category,A.BillNo,A.BillDetailId,A.WorkType ) AS T GROUP BY T.EmpId,Category ) AS T2 WHERE T2.Category = A.Category AND T2.EmpId = A.EmpId AND A.SumType = 'Total' SELECT E.FEmpNo AS EmpNo,A.Category AS Category, A.SumType AS SumType, ISNULL(P.FName,'') AS PayTypeName, ISNULL(A.PayAmt,0) AS PayAmt,ISNULL(A.Perf,0) AS Perf,ISNULL(A.DedCostPerf,0) AS DedCostPerf,ISNULL(A.Quantity,0) AS Quantity,ISNULL(A.Comm,0) AS Comm FROM #Employee AS E,#Summary AS A LEFT JOIN TPayAccount AS P ON P.FId = A.PayTypeId WHERE A.EmpId = E.FEmpId ORDER BY CASE WHEN ISNULL(P.FNo,'') = '' THEN '总' ELSE P.FNo END END GO