案例表达

发布于 2025-02-09 23:00:01 字数 229 浏览 0 评论 0原文

您如何编写具有2个条件的案例表达式?

我试图在下面的代码中说的是颜色是红色的,而质量不好,然后价格不佳。当颜色是红色但质量好时,价格 *2。

质量只有2个选项,即好和坏

代码示例:

Case when color = 'red' and quality = 'Bad' then price
     else price * 2
end as RED

How do you write a case expression that has 2 conditions ?

What I'm trying to say in the code below is when the color is red and the quality is bad then price. And when color is red but quality is Good then price *2.

There's only 2 options for quality i.e Good and Bad

Code Example:

Case when color = 'red' and quality = 'Bad' then price
     else price * 2
end as RED

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

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

发布评论

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

评论(2

悟红尘 2025-02-16 23:00:02

这是一个案例的基本结构:

case
    when A then X
    when B then Y
    else Z
end

您可以根据需要(或DBMS支持)拥有尽可能多的“ While/then”行。您可以将任何布尔表达式代替A或B。您可以将任何表达式代替X,Y和Z,包括另一种情况。

因此,您可以简单地列出所有这样的组合:

case
    when color = 'red' and quality = 'Good' then price*2
    when color = 'red' and quality = 'Bad' then price
    when color = 'blue' and quality = 'Good' then price*3
    when color = 'blue' and quality = 'Bad' then price/2
    else null
end as price

或者您可以这样嵌套它们:

case
    when color = 'red' then
        case
            when quality = 'Good' then price*2
            else price
        end
    when color = 'blue' then
        case
            when quality = 'Good' then price*3
            else price/2
        end
    else 
        null
end as price

Here is the basic structure of a CASE:

case
    when A then X
    when B then Y
    else Z
end

You can have as many "when/then" lines as you want (or your dbms supports). You can put any boolean expression in place of A or B. You can put any expression in place of X, Y, and Z, including another CASE.

So you could simply list out all your combinations like this:

case
    when color = 'red' and quality = 'Good' then price*2
    when color = 'red' and quality = 'Bad' then price
    when color = 'blue' and quality = 'Good' then price*3
    when color = 'blue' and quality = 'Bad' then price/2
    else null
end as price

Or you can nest them like this:

case
    when color = 'red' then
        case
            when quality = 'Good' then price*2
            else price
        end
    when color = 'blue' then
        case
            when quality = 'Good' then price*3
            else price/2
        end
    else 
        null
end as price
伴随着你 2025-02-16 23:00:02

考虑到您的示例:

select case color 
                when 'red' 
                     then (case quality when 'Bad' then price when 'Good' then price*2 end) 
                when 'white' 
                     then (case quality when 'Bad' then price+10 when 'Good' then (price+10)*2 end)
    --              [... other conditions ...]
                else 0 
            end as Price

查看案例的语法...结束可以以不同的方式使用。

case field when value1 then result1 when value2 then result2 ... else result0 end

case when field=value1 then result1 when field=value2 then result2 ... else result0 end

每个结果(Result1,result2,result0)本身可能是一个案例...结束语句。

Considering your example:

select case color 
                when 'red' 
                     then (case quality when 'Bad' then price when 'Good' then price*2 end) 
                when 'white' 
                     then (case quality when 'Bad' then price+10 when 'Good' then (price+10)*2 end)
    --              [... other conditions ...]
                else 0 
            end as Price

look at the syntax the case ... end can be used in differen ways.

case field when value1 then result1 when value2 then result2 ... else result0 end

or

case when field=value1 then result1 when field=value2 then result2 ... else result0 end

each results (result1, result2, result0) may itself be a case ... end statement.

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