在Sql Server中的单个case语句中组合多个条件 [英] Combining multiple condition in single case statement in Sql Server

查看:55
本文介绍了在Sql Server中的单个case语句中组合多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据下面的描述,我必须在 SQL server 中构建一个 CASE...END 语句,帮助我构建一个复杂的 CASE...END 语句到满足以下条件.

According to the following description I have to frame a CASE...END statement in SQL server , help me to frame a complex CASE...END statement to fulfill the following condition.

if PAT_ENT.SCR_DT is not null and PAT_ENTRY.ELIGIBILITY is null then display display 'Favor'
if PAT_ENT.SCR_DT is not null and PAT_ENTRY.EL is equal to No, display 'Error'
if PAT_ENTRY.EL is Yes and DS.DES is equal to null or OFF, display 'Active'
if DS.DES is equal to N, display 'Early Term'
if DS.DES is equal to Y, display 'Complete'

提前致谢.

推荐答案

你可以把条件放在 WHEN 子句之后,像这样:

You can put the condition after the WHEN clause, like so:

SELECT
  CASE
    WHEN PAT_ENT.SCR_DT is not null and PAT_ENTRY.ELIGIBILITY is null THEN 'Favor'
    WHEN PAT_ENT.SCR_DT is not null and PAT_ENTRY.EL = 'No' THEN 'Error'
    WHEN PAT_ENTRY.EL = 'Yes' and ISNULL(DS.DES, 'OFF') = 'OFF' THEN 'Active'
    WHEN DS.DES = 'N' THEN 'Early Term'
    WHEN DS.DES = 'Y' THEN 'Complete'
  END
FROM
  ....

当然,可以认为这样的复杂规则属于您的业务逻辑层,而不是数据库中的存储过程...

Of course, the argument could be made that complex rules like this belong in your business logic layer, not in a stored procedure in the database...

这篇关于在Sql Server中的单个case语句中组合多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆