SQL条件语句

发布于 2024-12-27 16:25:10 字数 690 浏览 0 评论 0原文

我有一个 Excel 工作表,下面有这个公式。我想用sql计算相同的公式。 Excel公式中有一个嵌套的if条件。用sql可以吗?我尝试过“Case .. When .. then .. Else ..”,但我无法管理!在我的 Excel 工作表中,这个计算结果是“OK”

谢谢,

Declare @ProjectName nvarchar(max)
Declare @NewTotalElapsedTimeEnd nvarchar(max)
Declare @TotalElapsedTime nvarchar(max)
Declare @SlaTime nvarchar(max)
Declare @Result nvarchar(max)

set @ProjectName = ''
set @NewTotalElapsedTimeEnd = 0
set @TotalElapsedTime = 69563
set @SlaTime = 86400

Excel 公式

=IF(ProjectName<>"","PROJECTED",IF(NewTotalElapsedTimeEnd=0,IF(TotalElapsedTime-SlaTime<0,"OK","NOK"),IF(NewTotalElapsedTimeEnd-SlaTime<0;"OK";"NOK")))

I have an Excel sheet and have this formula below. I would like to calculate the same formula with sql. In excel formula there is a nested if condition. Is it possible with sql ? I have tried with "Case .. When .. Then .. Else .." but I could not manage! In my excel sheet this calculation result is "OK"

Thank you,

Declare @ProjectName nvarchar(max)
Declare @NewTotalElapsedTimeEnd nvarchar(max)
Declare @TotalElapsedTime nvarchar(max)
Declare @SlaTime nvarchar(max)
Declare @Result nvarchar(max)

set @ProjectName = ''
set @NewTotalElapsedTimeEnd = 0
set @TotalElapsedTime = 69563
set @SlaTime = 86400

Excel Formula

=IF(ProjectName<>"","PROJECTED",IF(NewTotalElapsedTimeEnd=0,IF(TotalElapsedTime-SlaTime<0,"OK","NOK"),IF(NewTotalElapsedTimeEnd-SlaTime<0;"OK";"NOK")))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终遇你 2025-01-03 16:25:10

这应该对您有帮助(在 MS SQL Server 上完成,也许您的数据库系统需要对语法进行小幅更改)。 Case-when 工作正常,但您需要将时间跨度值作为数字才能进行减去。

干杯。

Declare @ProjectName nvarchar(max)
Declare @NewTotalElapsedTimeEnd int
Declare @TotalElapsedTime int
Declare @SlaTime int
Declare @Result nvarchar(max)

set @ProjectName = ''
set @NewTotalElapsedTimeEnd = 0
set @TotalElapsedTime = 69563
set @SlaTime = 86400

SET @Result =
    CASE WHEN @ProjectName <> ''
    THEN 'PROJECTED'
    ELSE
        CASE WHEN @NewTotalElapsedTimeEnd = 0
        THEN    
            CASE WHEN @TotalElapsedTime < 0
            THEN 'OK'
            ELSE 'NOK'
            END     
        ELSE
            CASE WHEN (@NewTotalElapsedTimeEnd - @SlaTime) < 0
            THEN 'OK'
            ELSE 'NOK'
            END
        END
    END

SELECT @Result

this should help you (done on MS SQL Server, maybe your database system needs small changes to syntax). Case-when is working fine, but you need to have your timespan values as a number for being able so substract.

Cheers.

Declare @ProjectName nvarchar(max)
Declare @NewTotalElapsedTimeEnd int
Declare @TotalElapsedTime int
Declare @SlaTime int
Declare @Result nvarchar(max)

set @ProjectName = ''
set @NewTotalElapsedTimeEnd = 0
set @TotalElapsedTime = 69563
set @SlaTime = 86400

SET @Result =
    CASE WHEN @ProjectName <> ''
    THEN 'PROJECTED'
    ELSE
        CASE WHEN @NewTotalElapsedTimeEnd = 0
        THEN    
            CASE WHEN @TotalElapsedTime < 0
            THEN 'OK'
            ELSE 'NOK'
            END     
        ELSE
            CASE WHEN (@NewTotalElapsedTimeEnd - @SlaTime) < 0
            THEN 'OK'
            ELSE 'NOK'
            END
        END
    END

SELECT @Result
悟红尘 2025-01-03 16:25:10

是的,您可以嵌套 CASE 语句,如下所示:

CASE WHEN ProjectName <> '' THEN 'PROJECTED'
ELSE
    CASE WHEN NewTotalElapsedTimeEnd = 0 THEN
            CASE WHEN TotalElapsedTime - SlaTime < 0 THEN 'OK' 
            ELSE 'NOK' END
        ELSE
            CASE WHEN NewTotalElapsedTimeEnd - SlaTime < 0 THEN 'OK' 
            ELSE 'NOK' END
        END
END

Yes, you can nest CASE statements, something like this:

CASE WHEN ProjectName <> '' THEN 'PROJECTED'
ELSE
    CASE WHEN NewTotalElapsedTimeEnd = 0 THEN
            CASE WHEN TotalElapsedTime - SlaTime < 0 THEN 'OK' 
            ELSE 'NOK' END
        ELSE
            CASE WHEN NewTotalElapsedTimeEnd - SlaTime < 0 THEN 'OK' 
            ELSE 'NOK' END
        END
END
三五鸿雁 2025-01-03 16:25:10

首先,缩进 IF:

=IF(ProjectName<>"",
    "PROJECTED",
     IF(NewTotalElapsedTimeEnd=0,
        IF(TotalElapsedTime-SlaTime<0,
            "OK",
            "NOK"),
        IF(NewTotalElapsedTimeEnd-SlaTime<0;"OK";"NOK")))

其次,将其转换为 CASE:

    case when ProjectName = "" 
    then "PROJECTED"
    else case when NewTotalElapsedTimeEnd=0 
        then case when TotalElapsedTime-SlaTime<0
             then "OK"
             else "NOK"
             end 
        else case when NewTotalElapsedTimeEnd-SlaTime<0
             then "OK"
             else "NOK"
             end
        end
     end

first, indent your IF:

=IF(ProjectName<>"",
    "PROJECTED",
     IF(NewTotalElapsedTimeEnd=0,
        IF(TotalElapsedTime-SlaTime<0,
            "OK",
            "NOK"),
        IF(NewTotalElapsedTimeEnd-SlaTime<0;"OK";"NOK")))

second, translate it to CASE:

    case when ProjectName = "" 
    then "PROJECTED"
    else case when NewTotalElapsedTimeEnd=0 
        then case when TotalElapsedTime-SlaTime<0
             then "OK"
             else "NOK"
             end 
        else case when NewTotalElapsedTimeEnd-SlaTime<0
             then "OK"
             else "NOK"
             end
        end
     end
以歌曲疗慰 2025-01-03 16:25:10

这是你的嵌套 if 。

IF (ProjectName<>"",
      "PROJECTED",
      IF(NewTotalElapsedTimeEnd=0,
          IF(TotalElapsedTime-SlaTime<0,
              "OK",
              "NOK"
          ),
          IF(NewTotalElapsedTimeEnd-SlaTime<0;
              "OK";
              "NOK"
          )

      )
 )

您可以使用 T-SQL IF 执行此操作:

IF @ProjectName<>""
      set @Result ="PROJECTED"
ELSE
      IF @NewTotalElapsedTimeEnd=0
          IF @TotalElapsedTime-@SlaTime<0
              set @Result = "OK"
          ELSE
              set @Result = "NOK"
      ELSE              
          IF @NewTotalElapsedTimeEnd-@SlaTime<0
              set @Result = "OK"
          ELSE
              set @Result = "NOK"

This is your nested if.

IF (ProjectName<>"",
      "PROJECTED",
      IF(NewTotalElapsedTimeEnd=0,
          IF(TotalElapsedTime-SlaTime<0,
              "OK",
              "NOK"
          ),
          IF(NewTotalElapsedTimeEnd-SlaTime<0;
              "OK";
              "NOK"
          )

      )
 )

You can do this with T-SQL IF:

IF @ProjectName<>""
      set @Result ="PROJECTED"
ELSE
      IF @NewTotalElapsedTimeEnd=0
          IF @TotalElapsedTime-@SlaTime<0
              set @Result = "OK"
          ELSE
              set @Result = "NOK"
      ELSE              
          IF @NewTotalElapsedTimeEnd-@SlaTime<0
              set @Result = "OK"
          ELSE
              set @Result = "NOK"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文