SQL条件语句
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该对您有帮助(在 MS SQL Server 上完成,也许您的数据库系统需要对语法进行小幅更改)。 Case-when 工作正常,但您需要将时间跨度值作为数字才能进行减去。
干杯。
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.
是的,您可以嵌套 CASE 语句,如下所示:
Yes, you can nest CASE statements, something like this:
首先,缩进 IF:
其次,将其转换为 CASE:
first, indent your IF:
second, translate it to CASE:
这是你的嵌套 if 。
您可以使用 T-SQL IF 执行此操作:
This is your nested if.
You can do this with T-SQL IF: