tsql:是否可以在 select 中执行嵌套 case 语句?

发布于 2024-09-27 16:12:13 字数 610 浏览 2 评论 0原文

如何将嵌套 if 语句合并到 sql 查询的 select 子句中? 我知道在条件 then X else y 结束时使用 case,但是如何以相同的方式为记录集中的每条记录执行嵌套操作。

if x.boy is not null then 
   x.boy 
else if x.girl is not null
   then x.girl 
else if x.dog is not null 
   then x.dog
else 
    x.cat 

这是我的尝试:

SELECT top 10
        id,
        case when x.boy <> NULL then 
            x.boy
        else case when  x.girl <> NULL 
            x.girl
                else case when  x.dog <> NULL 
            x.dog
                else x.cat 

        end as Who 
from house x

这是正确的吗?

how do i incorporate a nested if statement in a select clause of a sql query?
I know to use case when condition then X else y end but how do you do a nested one in the same fashion for each record in a record set.

if x.boy is not null then 
   x.boy 
else if x.girl is not null
   then x.girl 
else if x.dog is not null 
   then x.dog
else 
    x.cat 

here is my attempt:

SELECT top 10
        id,
        case when x.boy <> NULL then 
            x.boy
        else case when  x.girl <> NULL 
            x.girl
                else case when  x.dog <> NULL 
            x.dog
                else x.cat 

        end as Who 
from house x

is this correct?

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

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

发布评论

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

评论(3

鹤仙姿 2024-10-04 16:12:13

是的。案件中的案件并没有什么问题。

虽然,这是你的脚本,正确编写:

SELECT top 10
    id,
    case
        when x.boy IS NOT NULL then x.boy
        else case
            when x.girl IS NOT NULL THEN x.girl
            else case
                when x.dog IS NOT NULL THEN x.dog
                else x.cat
            end
        end
    end as Who 
from house x

OR

SELECT top 10
    id,
    case
        when x.boy IS NOT NULL then x.boy
        when x.girl IS NOT NULL THEN x.girl
        when x.dog IS NOT NULL THEN x.dog
        else x.cat
    end as Who 
from house x

OR

SELECT top 10
    id,
    coalesce(x.boy, x.girl, x.dog, x.cat) AS Who
from house x

Yes. There is nothing wrong with a case within a case.

Although, here is your script, written corectly:

SELECT top 10
    id,
    case
        when x.boy IS NOT NULL then x.boy
        else case
            when x.girl IS NOT NULL THEN x.girl
            else case
                when x.dog IS NOT NULL THEN x.dog
                else x.cat
            end
        end
    end as Who 
from house x

OR

SELECT top 10
    id,
    case
        when x.boy IS NOT NULL then x.boy
        when x.girl IS NOT NULL THEN x.girl
        when x.dog IS NOT NULL THEN x.dog
        else x.cat
    end as Who 
from house x

OR

SELECT top 10
    id,
    coalesce(x.boy, x.girl, x.dog, x.cat) AS Who
from house x
记忆之渊 2024-10-04 16:12:13

您可以使用COALESCE 来简化此操作。

SELECT TOP 10 id, COALESCE(x.boy, x.girl, x.dog, x.cat) as Who
    FROM house x

You could simplify this with COALESCE.

SELECT TOP 10 id, COALESCE(x.boy, x.girl, x.dog, x.cat) as Who
    FROM house x
寄与心 2024-10-04 16:12:13

尝试一下:

SELECT top 10
        id,
        case when x.boy Is Not NULL then x.boy
        else 
              case when  x.girl Is Not NULL then x.girl
              else 
                    case when  x.dog Is Not Null Then x.dog
                    else x.cat End
               End

        end as Who 
from house x

尽管您可以按照 Joe 的建议使用 coalesce

Try this out:

SELECT top 10
        id,
        case when x.boy Is Not NULL then x.boy
        else 
              case when  x.girl Is Not NULL then x.girl
              else 
                    case when  x.dog Is Not Null Then x.dog
                    else x.cat End
               End

        end as Who 
from house x

although you could just use coalesce as Joe suggested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文