Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

sql - 使用SQL Server 2008如何处理多种情况?(How do I do multiple CASE WHEN conditions using SQL Server 2008?)

What I'm trying to do is use more than one CASE WHEN condition for the same column.

(我想做的是对同一列使用多个CASE WHEN条件。)

Here is my code for the query:

(这是我的查询代码:)

   SELECT   Url='',
            p.ArtNo,
            p.[Description],
            p.Specification,
            CASE 
            WHEN 1 = 1 or 1 = 1 
               THEN 1 
               ELSE 0 
            END as Qty,
            p.NetPrice,
            [Status] = 0
      FROM  Product p (NOLOCK)

However, what I want to do is use more then one WHEN for the same column "qty".

(但是,我要对同一列“ qty”使用多个WHEN。)

As in the following code:

(如以下代码所示:)

IF
// CODE
ELSE IF
// CODE
ELSE IF
// CODE
ELSE
// CODE
  ask by Nils Anders translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two formats of case expression .

(案例表达两种格式 。)

You can do CASE with many WHEN as;

(您可以使用多个WHEN作为CASE ;)

CASE  WHEN Col1 = 1 OR Col3 = 1  THEN 1 
      WHEN Col1 = 2 THEN 2
      ...
      ELSE 0 END as Qty

Or a Simple CASE expression

(简单的CASE表达式)

CASE Col1 WHEN 1 THEN 11 WHEN 2 THEN 21 ELSE 13 END

Or CASE within CASE as;

( CASE 中的 CASE为;)

CASE  WHEN Col1 < 2 THEN  
                    CASE Col2 WHEN 'X' THEN 10 ELSE 11 END
      WHEN Col1 = 2 THEN 2
      ...
      ELSE 0 END as Qty

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...