USE [OnlineQualDB] GO /****** Object: StoredProcedure [dbo].[usp_Tqc_QualStat4] Script Date: 10/25/2013 16:14:54 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> EXEC usp_Tqc_QualStat4 -- 17.5904 -- ============================================= ALTER PROCEDURE [dbo].[usp_Tqc_QualStat4] AS BEGIN SET NOCOUNT ON ; --第1个游标变量 DECLARE @ProcessID UNIQUEIDENTIFIER , @ProcessName1 VARCHAR(50) , @ProcessCode VARCHAR(50) , @MaxRecordTime DATETIME --第2个游标变量 DECLARE @ProcessName2 VARCHAR(50) --第3个游标变量 DECLARE @RowNumber INT , @ProcessName VARCHAR(50) , @TakeTime DATETIME --上一条记录时间,下一条记录时间 DECLARE @RecordTime1 DATETIME , @RecordTime2 DATETIME --水分各工序最大时间 DECLARE @RecordTime DATETIME --取得在线水分数据并放入#t1 SELECT t1.* , t2.F_ProcessName AS ProcessName , t2.F_Code INTO #t1 FROM T_QualMoisture_Online t1 INNER JOIN T_DIC_QualProcess t2 ON t1.ProcessID = T2.F_ProcessID --SELECT * --FROM #t1 -- 分工序取得在线水分结构参数系数,记录数以及权重 CREATE TABLE #t2 ( RowNumber INT , N_Slow_Value NUMERIC(18, 2) , T_Take_Time DATETIME , Weight_Coefficient NUMERIC(18, 2) , ProcessName VARCHAR(50) ) --结果集 DECLARE @t TABLE ( ProcessName VARCHAR(20) , RowNumber INT , Value NUMERIC(18, 4) ) DECLARE Process_cur CURSOR FOR --第1个游标 SELECT t1.ProcessID,t2.F_ProcessName AS ProcessName, t2.F_Code AS ProcessCode, MaxRecordTime FROM ( SELECT ProcessID,MAX(RecordTime) AS MaxRecordTime FROM T_QualMoisture_Online GROUP BY ProcessID) t1 LEFT JOIN T_DIC_QualProcess t2 ON t1.ProcessID = T2.F_ProcessID OPEN Process_cur FETCH NEXT FROM Process_cur INTO @ProcessID,@ProcessName1,@ProcessCode, @MaxRecordTime WHILE ( @@fetch_status = 0 ) BEGIN PRINT '@ProcessCode: ' + CONVERT(VARCHAR(50), @ProcessCode) -- 循环分工序取得在线水分各工序最大时间 SELECT @RecordTime = ( SELECT MaxRecordTime FROM ( SELECT ProcessID , MAX(RecordTime) AS MaxRecordTime FROM T_QualMoisture_Online GROUP BY ProcessID ) t1 LEFT JOIN T_DIC_QualProcess t2 ON t1.ProcessID = T2.F_ProcessID WHERE t2.F_Code = @ProcessCode ) -- 取得权重系数 INSERT INTO #t2 SELECT RowNumber , N_Slow_Value , T_Take_Time , Weight_Coefficient , t3.F_ProcessName AS ProcessName FROM T_QualMoisture_Online_Parameter t1 INNER JOIN ( SELECT ROW_NUMBER() OVER ( ORDER BY T_Take_Time DESC ) AS RowNumber , T_Take_Time , N_Slow_Value , b.F_ProcessID AS ProcessID FROM dbo.T_QualMoisture_Middle_Detail a INNER JOIN T_DIC_QualProcess b ON a.V_CaseNumber = b.F_Code WHERE T_Take_Time < @RecordTime AND N_Slow_Value IS NOT NULL AND V_CaseNumber = @ProcessCode ) t2 ON t1.Sample_Index = t2.RowNumber AND t1.ProcessID = t2.ProcessID INNER JOIN T_DIC_QualProcess t3 ON t1.ProcessID = t3.F_ProcessID FETCH NEXT FROM Process_cur INTO @ProcessID,@ProcessName1,@ProcessCode, @MaxRecordTime END CLOSE Process_cur DEALLOCATE Process_cur --SELECT * FROM #t2 DECLARE GroupProcessName_cur CURSOR FOR --第二个游标开始,这里运用了游标嵌套 SELECT ProcessName FROM #t2 GROUP BY ProcessName OPEN GroupProcessName_cur FETCH NEXT FROM GroupProcessName_cur INTO @ProcessName2 WHILE ( @@fetch_status = 0 ) BEGIN PRINT '@ProcessName: ' + CONVERT(VARCHAR(50), @ProcessName2) DECLARE Moisture_cur CURSOR FOR --第3个游标开始 SELECT RowNumber, ProcessName,T_Take_Time FROM #t2 WHERE ProcessName = @ProcessName2 OPEN Moisture_cur FETCH NEXT FROM Moisture_cur INTO @RowNumber,@ProcessName,@TakeTime WHILE ( @@fetch_status = 0 ) BEGIN PRINT '@RowNumber: ' + CONVERT(VARCHAR(50), @RowNumber) PRINT '@ProcessName: ' + CONVERT(VARCHAR(50), @ProcessName) PRINT '@TakeTime: ' + CONVERT(VARCHAR(50), @TakeTime) PRINT '--------------------------' --取上一条记录RowNumber SELECT @RecordTime1 = ( SELECT TOP 1 T_Take_Time FROM #t2 WHERE RowNumber < @RowNumber AND ProcessName = @ProcessName2 ORDER BY RowNumber DESC ) IF ( @RecordTime1 IS NULL ) SET @RecordTime1 = @TakeTime PRINT @RecordTime1 --取下一条记录RowNumber SELECT @RecordTime2 = ( SELECT TOP 1 T_Take_Time FROM #t2 WHERE RowNumber > @RowNumber AND ProcessName = @ProcessName2 ORDER BY RowNumber ASC ) IF ( @RecordTime2 IS NULL ) SET @RecordTime2 = @TakeTime PRINT @RecordTime2 INSERT INTO @t SELECT @ProcessName , @RowNumber , AVG(Value) AS Value FROM #t1 WHERE RecordTime BETWEEN @RecordTime2 AND @RecordTime1 AND ProcessName = @ProcessName2 GROUP BY ProcessName FETCH NEXT FROM Moisture_cur INTO @RowNumber,@ProcessName,@TakeTime END CLOSE Moisture_cur DEALLOCATE Moisture_cur--第3个游标结束 FETCH NEXT FROM GroupProcessName_cur INTO @ProcessName2 END CLOSE GroupProcessName_cur DEALLOCATE GroupProcessName_cur--第二个游标结束 --SELECT * --FROM @t SELECT t1.ProcessName , t1.RowNumber , t1.Value , N_Slow_Value , T_Take_Time , Weight_Coefficient , ( Value - N_Slow_Value ) * Weight_Coefficient AS Result INTO #t3 FROM @t t1 INNER JOIN #t2 t2 ON t1.RowNumber = t2.RowNumber AND t1.ProcessName = t2.ProcessName --SELECT * --FROM #t3 SELECT F_ProcessName AS ProcessName , Value AS LastValue , ISNULL(Result, 0) AS Ratio , Value - ISNULL(Result, 0) AS ReviseValue FROM ( SELECT * , ROW_NUMBER() OVER ( PARTITION BY ProcessID ORDER BY RecordTime DESC ) rn FROM T_QualMoisture_Online ) t INNER JOIN T_DIC_QualProcess t2 ON t.ProcessID = T2.F_ProcessID LEFT JOIN ( SELECT ProcessName , SUM(Result) AS Result FROM #t3 GROUP BY ProcessName ) t3 ON t2.F_ProcessName = t3.ProcessName WHERE rn = 1 DROP TABLE #t1 DROP TABLE #t2 DROP TABLE #t3 END GO