SQL CASE 语句

发布于 2024-10-30 02:04:52 字数 385 浏览 1 评论 0原文

我的数据库表 PE 和 PER 有两个字段(BIT 数据类型),我需要编写一条 sql case 语句来执行以下任务。

当 PER=0 时则打印“不需要” 当 PER=1 时,检查 PE=0,然后打印“未完成” 当 PE=1 时,打印“已完成”

以下是我尝试过的查询,但该查询不起作用

CASE pc.PER WHEN '0' THEN 'Not Required'
                                    WHEN '1' THEN (CASE pc.PE WHEN '0' THEN 'Not Completed'
                       WHEN '1' THEN 'Completed') end as ab 

There are two fields (BIT data type) in my database table PE and PER i need to write a sql case statement to perform the following task.

when PER=0 then print 'Not Required'
when PER=1 then check if PE=0 then print 'Not Completed' when PE=1 print 'Completed'

following is the query i have tried and which is not working

CASE pc.PER WHEN '0' THEN 'Not Required'
                                    WHEN '1' THEN (CASE pc.PE WHEN '0' THEN 'Not Completed'
                       WHEN '1' THEN 'Completed') end as ab 

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

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

发布评论

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

评论(3

篱下浅笙歌 2024-11-06 02:04:53

你错过了一个结局。

CASE pc.PER 
    WHEN 0 THEN 'Not Required'
    WHEN 1 THEN 
        CASE pc.PE 
            WHEN 0 THEN 'Not Completed'
            WHEN 1 THEN 'Completed'
        END
END as ab 

You are missing an end.

CASE pc.PER 
    WHEN 0 THEN 'Not Required'
    WHEN 1 THEN 
        CASE pc.PE 
            WHEN 0 THEN 'Not Completed'
            WHEN 1 THEN 'Completed'
        END
END as ab 
感情旳空白 2024-11-06 02:04:53

尝试使用以下语句:

CASE WHEN pc.PER =0 THEN 'Not Required'  
     WHEN pc.PER =1 THEN 
         CASE   WHEN pc.PE = 0 THEN 'Not Completed'
                WHEN pc.PE = 1 THEN 'Completed' 
     End 
End as ab

try the following statement instead:

CASE WHEN pc.PER =0 THEN 'Not Required'  
     WHEN pc.PER =1 THEN 
         CASE   WHEN pc.PE = 0 THEN 'Not Completed'
                WHEN pc.PE = 1 THEN 'Completed' 
     End 
End as ab
未央 2024-11-06 02:04:52

我认为将其写为更容易:

CASE
    WHEN pc.PER = '0' THEN 'Not Required'
    WHEN pc.PER = '1' AND pc.PE = '0' THEN 'Not Completed'
    ELSE 'Completed'
END

或者,您必须在查询中的 ... WHEN '1' THEN 'Completed' 之后添加 END

CASE pc.PER WHEN '0' THEN 'Not Required'
            WHEN '1' THEN (CASE pc.PE WHEN '0' THEN 'Not Completed'
                                      WHEN '1' THEN 'Completed'
                           END)
END AS ab 

I think it might be easier to write it as:

CASE
    WHEN pc.PER = '0' THEN 'Not Required'
    WHEN pc.PER = '1' AND pc.PE = '0' THEN 'Not Completed'
    ELSE 'Completed'
END

Alternatively you would have to add END after ... WHEN '1' THEN 'Completed' in your query:

CASE pc.PER WHEN '0' THEN 'Not Required'
            WHEN '1' THEN (CASE pc.PE WHEN '0' THEN 'Not Completed'
                                      WHEN '1' THEN 'Completed'
                           END)
END AS ab 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文