需要在我的代码中包含if else语句

时间:2014-03-20 20:15:38

标签: sql sql-server sql-server-2008 reporting-services

我正在尝试在下面的代码中实现if else语句或case语句。我想使用其中一个语句(if或case)来查看我的RT_Ch_Pres_PX1值是否在某些最小或最大规格内,如果它们高于或低于我想说如果我的RT_Ch_Pres_PX1低于我的最小值那么多那么显示该值并表示超过最大值的数量和相同数量。例如,如果我的RT_Ch_Pres_PX1值为5,并且我想使用我的最小值为6,最大值为10.所以我的Rt_Ch_pres_px1值减1,所以我想显示这个并说这个值是1的值。如果RT_Ch_Pres_PX1在min内,则max值不执行任何操作。请参阅下面的代码。

                    DECLARE @Result TABLE
            (
                        RT_DateTime datetime,
                        RT_Phase_Name varchar(30),
                        RT_PhaseChangeCount int,
                        RT_Phase_Type int,
                        RT_Ch_Pres_PX1 float           
            );
            /* Variables used to track changes to Phase Name */
            DECLARE @RT_DateTime datetime;
            DECLARE @RT_Phase_Name varchar(30);
            DECLARE @RT_PhaseChangeCount int;
            DECLARE @RT_Phase_Type int;
            DECLARE @RT_Ch_Pres_PX1 float;
            DECLARE @PhaseNameHold varchar(30);
            DECLARE @PhaseChangeCount int;
            SELECT @PhaseNameHold = ' ';
            SELECT @PhaseChangeCount = 0;
            SELECT @RT_PhaseChangeCount = 0;

            /* Declare a cursor for determining when Phases change */

            DECLARE ImportCursor CURSOR FAST_FORWARD FOR

            SELECT 
            CONVERT(datetime, dbo.CycleData.Date_Time) as TimeConvert, 
            [dbo].[LookupPhases].[Phase_Name],
            [dbo].[cycledata].[phase_type],
            [dbo].[cycledata].[Ch_Pres_PX1]

            FROM
             CycleData INNER JOIN
                                     CycleDataHeader ON CycleData.Unit_Number = CycleDataHeader.Unit_Number AND CycleData.Cycle_Counter_No = CycleDataHeader.Cycle_Counter_No INNER JOIN
                                     LookupPhases ON CycleData.Phase_Type = LookupPhases.Phase_Type INNER JOIN
                                     LookupEvent ON CycleData.Event_Type = LookupEvent.Event_Id LEFT OUTER JOIN
                                     LookupAlarm ON CycleData.Alarm_Type = LookupAlarm.Alarm_Id

              WHERE 
            [dbo].[CycleDataHeader].[Entered_Load_No1] = 'T14-0008'


            ORDER BY

            /* Appears to be the order that needs to be reported on */

            Cycle_Time
            -- dbo.CycleData.Unit_Number,

            -- TimeConvert;

            OPEN ImportCursor;

            FETCH NEXT FROM ImportCursor INTO @RT_DateTime, 
                                                @RT_Phase_Name,
                                                @RT_Phase_Type,
                                                @RT_Ch_Pres_PX1  
            WHILE @@FETCH_STATUS = 0
            BEGIN
            IF (@RT_Phase_Name <> @PhaseNameHold)   
                BEGIN
                                    SET @PhaseNameHold = @RT_Phase_Name;
                                    SET @RT_PhaseChangeCount = @RT_PhaseChangeCount + 1;
                                    END
                        INSERT INTO @Result VALUES(@RT_DateTime, @RT_Phase_Name,@RT_PhaseChangeCount,@RT_Phase_Type,@RT_Ch_Pres_PX1);           
                        FETCH NEXT FROM ImportCursor INTO @RT_DateTime, @RT_Phase_Name,@RT_Phase_Type,@RT_Ch_Pres_PX1;
            END
            CLOSE ImportCursor;
            DEALLOCATE ImportCursor;
            SELECT
                        RT_DateTime,
                        RT_Phase_Name,
                        RT_PhaseChangeCount,
                        RT_Phase_Type,
                        RT_Ch_Pres_PX1 


            FROM @Result;

1 个答案:

答案 0 :(得分:0)

此案例将生成您想要的值:

case
  when RT_Ch_Pres_PX1 < some_min then RT_Ch_Pres_PX1 - some_min
  when RT_Ch_Pres_PX1 > some_max then RT_Ch_Pres_PX1 - some_max
  else 0
end

为下冲创建的值是负数(我认为这是一个好主意)。如果您希望它是正数,请翻转计算。