English 中文(简体)
在有2个不同领域的“个案”声明中使用“类似”
原标题:Using "like" inside a "case" statement with 2 different fields

我的议席上有2个领域,我需要说明。

Col1          Col2     Col3
abc.txt       Y        0
def.txt       N        0
bbck.txt      Y        1

The Col3 values are based on the Col1 and Col2 values in the following manner.
Y = 1 and N = 0. So all the values in Col2 that are Y shall become 1 in col3, and Nin Col2 will become 0 in Col3, UNLESS the col1 value ends with %c.txt. As you can see since the abc.txt ends with %c.txt the value in col3 becomes 0.

我知道,可以通过一份<代码>CASE的声明来做到这一点。 是否有任何人知道如何?

我的法典

SELECT 
  CASE Col2
    WHEN  Y  THEN  1 
    WHEN  N  THEN  0 
  ELSE
(CASE WHEN [Col1] LIKE  %c.txt  THEN  0 
 END)
  END
  AS Col3,
 *
FROM Tabl1

希望这一想法

最佳回答

或许:

SELECT 
    CASE 
      WHEN Col2 =  N 
        OR Col1 LIKE  %c.txt 
          THEN  0 
      WHEN Col2 =  Y 
          THEN  1 
    END AS Col3
  , * 
FROM Tabl1
问题回答
SELECT 
  CASE WHEN Col2 LIKE  %c.txt  THEN  0 
       ELSE
       CASE 
            WHEN Col2  =  N   THEN  0 
            WHEN Col2  =  Y  THEN  1 
       END
  END AS COL3
FROM Tabl1

我并不认为你的幽灵是清楚的,例如,在“联合国升温潜能值”一词之前的 com是否假定以下条款适用于之前的条款?

似乎有四种可能的组合:

1) Col1 LIKE  %c.txt 
   AND Col2 =  Y 

2) Col1 LIKE  %c.txt 
   AND Col2 =  N 

3) Col1 NOT LIKE  %c.txt 
   AND Col2 =  Y 

4) Col1 NOT LIKE  %c.txt 
   AND Col2 =  N 

你的话

一、需要案件陈述

情况如何? 同样,总是能够以多种方式实现(这就是为什么如此难以建立一个更优化的平台:) 我认为,为了执行《<><><>>,需要一个幽灵,而不是对<>strong>施加不合理的限制。

以下用途:<条码>UNION,而不是例例,如果您从一开始就认为了这一点,那么你的幽灵会更好;]

WITH Tabl1
     AS 
     (
      SELECT * 
        FROM (
              VALUES ( abc.txt ,  Y ),
                     ( def.txt ,  N ),
                     ( bbck.txt ,  Y ),
                     ( disc.txt ,  N )
             ) AS T (Col1, Col2)
     )
SELECT  0  AS Col3, *
  FROM Tabl1
 WHERE Col1 LIKE  %c.txt 
       AND Col2 =  Y 
UNION
SELECT  0  AS Col3, *
  FROM Tabl1
 WHERE Col1 LIKE  %c.txt 
       AND Col2 =  N 
UNION
SELECT  1  AS Col3, *
  FROM Tabl1
 WHERE Col1 NOT LIKE  %c.txt 
       AND Col2 =  Y 
UNION
SELECT  0  AS Col3, *
  FROM Tabl1
 WHERE Col1 NOT LIKE  %c.txt 
       AND Col2 =  N ;

P.S. I. P. P.S. P. P.S. P.,这是为了与接受的答复相匹配,但我不相信这是正确的!

This might be what your looking for:

 SELECT * FROM Tabl1
 cross apply
    (
      select Col3 =
      CASE  
            WHEN Tabl1.Col2 =  Y  then  1 
            WHEN Tabl1.Col2 =  Y  then  1 
            WHEN RIGHT(Tabl1.Col1, 6) =  c.txt  then  0 
            END 
) as Col3




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签