English 中文(简体)
传真机
原标题:SQL Server Case when syntax

Ive a Great selected case in Prof.

(select case tb_usuario.int_id_cargo 
                    when 13
                    then  20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53 
                    when 20
                    then   
                    when 21
                    then  20 
                    when 22
                    then   
                    when 23
                    then  22 
                    when 24
                    then  20,21,22,23 
                    when 25
                    then  20,21,22,23,24 
                    when 26
                    then  20,21,22,23,24,25 
                    when 27
                    then   
                    when 28
                    then  27 
                    when 29
                    then   
                    when 30
                    then  29 
                    when 31
                    then  27,28 
                    when 32
                    then  27,28,31 
                    when 33
                    then  29,30 
                    when 34
                    then  27,28,29,30,31,32,33 
                    when 35
                    then   
                    else   
                    end) as subs

如果你注意的话,你会认识到,有许多案例会恢复空洞。 我要检查那些在单一案件中返回空洞的人,即:

  (select case tb_usuario.int_id_cargo 
                        when 20,22,27,29,35
                                        then   
                                         [...]

这是可能的吗?

最佳回答

如何使用表格(临时表格)在不使用<编码>CASE说明的情况下选定这些数值?

migration (table name)
old_id      new_id
---------   --------------
24          20,21,22,23
30          29
.....

您的问候会

select migration.new_id 
from migration inner join tb_usuario
on migration.old_id = tb_usuario.int_id_cargo

EDIT:对于多旧的_指新_和空白str,你必须单独制作条目。

e.g. (假设旧的1 &2点指新_id->空白)

migration (table name)
old_id      new_id
---------   --------------
1          
2

这样做的好处是,将新金从空白改为使用本表的其他东西,而不是改变上文提到的询问。

问题回答

我将把它留给SE条款。 仅指定您希望填写在CASE上。 除非你有理由在案件陈述中保留这些身份证。

(select case tb_usuario.int_id_cargo 
                    when 13
                    then  20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53 
                    when 21
                    then  20 
                    when 23
                    then  22 
                    when 24
                    then  20,21,22,23 
                    when 25
                    then  20,21,22,23,24 
                    when 26
                    then  20,21,22,23,24,25 
                    when 28
                    then  27 
                    when 30
                    then  29 
                    when 31
                    then  27,28 
                    when 32
                    then  27,28,31 
                    when 33
                    then  29,30 
                    when 34
                    then  27,28,29,30,31,32,33 
                    else   
                    end) as subs

使用:

(SELECT CASE 
         WHEN tb_usuario.int_id_cargo = 13 THEN
           20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53 
         WHEN tb_usuario.int_id_cargo IN(20, 22, 27, 29, 35) THEN
              
         WHEN tb_usuario.int_id_cargo = 21 THEN
             20 
         WHEN tb_usuario.int_id_cargo = 23 THEN
             22 
         WHEN tb_usuario.int_id_cargo = 24 THEN
             20,21,22,23 
         WHEN tb_usuario.int_id_cargo = 25 THEN
             20,21,22,23,24 
         WHEN tb_usuario.int_id_cargo = 26 THEN
             20,21,22,23,24,25 
         WHEN tb_usuario.int_id_cargo = 28 THEN
             27 
         WHEN tb_usuario.int_id_cargo = 30 THEN
             29 
         WHEN tb_usuario.int_id_cargo = 31 THEN
             27,28 
         WHEN tb_usuario.int_id_cargo = 32 THEN
             27,28,31 
         WHEN tb_usuario.int_id_cargo = 33 THEN
            29,30 
         WHEN tb_usuario.int_id_cargo = 34 THEN
            27,28,29,30,31,32,33 
         ELSE   
        END) as subs

尽管我看不出区分应当恢复零长度的特有价值,而只是让ELSE(回报同样的价值)达到这些价值:

(SELECT CASE tb_usuario.int_id_cargo 
         WHEN 13 THEN
            20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53 
         WHEN 21 THEN
             20 
         WHEN 23 THEN
             22 
         WHEN 24 THEN
             20,21,22,23 
         WHEN 25 THEN
             20,21,22,23,24 
         WHEN 26 THEN
             20,21,22,23,24,25 
         WHEN 28 THEN
             27 
         WHEN 30 THEN
             29 
         WHEN 31 THEN
             27,28 
         WHEN 32 THEN
             27,28,31 
         WHEN 33 THEN
            29,30 
         WHEN 34 THEN
            27,28,29,30,31,32,33 
         ELSE   
        END) as subs
select (case 
       when tbl_usuario.int_id_cargo in (20,22,27,29,35,.....) then    
       else
            case tbl_usuario.int_id_cargo
            when 23 then  22 
            when 25 then  20,21,22,23,24 
            when ....
            end
        end) as subs

replace .... with the rest of the ids and then fix the second case statement so that matches up for all the other values





相关问题
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: ...

热门标签