【问题标题】:SQL Multiple IF statements based on conditionsSQL 基于条件的多个 IF 语句
【发布时间】:2023-01-12 02:13:39
【问题描述】:

Oracle SQL 中计算值并根据现有列的值和其他条件存储它们的最佳方法是什么。

例如:

如果第 1 列的值为 =>3,则结果应默认为该列的值。

但是,如果第 1 列的值 <3 并且第 2 列的值填充为 1 并且第 3 列、第 4 列和第 5 列都有值,那么该值应默认为 2

如果第 1 列的值 < 3 并且第 2 列的值填充为 1 第 4 列、第 5 列有值,但第 3 列没有,则默认值为 2

如果第 1 列的值 < 3 并且第 2 列的值填充为 1 第 3 列和第 4 列有值,但第 5 列没有,则默认值为 1 ...等等,用于填充第 3、4、5 列的所有场景

如果第 1 列的值 < 3 并且第 2 列的值填充为 2 并且第 3 列、第 4 列和第 5 列都有值,那么该值应默认为 3 等等所有场景。

只想知道制定它的最简单方法,因为有很多条件(确切地说是 19 个条件)

【问题讨论】:

  • 可能是一个 case 表达式,每组条件都有一个分支,按照它们需要评估的顺序。当您说“存储它们”时,您的意思是作为同一张表中的另一列吗?如果是这样,请查看虚拟列。不确定你为什么要标记 PL/SQL——不清楚你想在何时何地解决这个问题……

标签: sql oracle plsql


【解决方案1】:

除了您的一般判断力之外,我会使用一个案例,将您的第一个标准放在第一位,然后继续处理后续的下一个优先级条件。

-- column1 is >= 3, use it as first.
case when column1 >= 3
          then column1
-- if it gets this far, then column1 is LESS than 3
     when column1 < 3
      AND column2 = 1
      AND column3 = column4 
      AND column3 = column5
     then column2

    when column1 < 3
      AND column2 = 1
      etc
    then ...
    else ... last choice
end

将 WHEN 条件置于应处理的优先级。

【讨论】:

    【解决方案2】:

    我不知道你的一些条件是什么(“等等”),但其中一些(如 2 和 3)是相同的。我怀疑你有19个条件。

    您需要知道哪些条件具有优先权。

    下面是基于您提供的查询的基本代码结构。

    select
      case
        when col1 >= 3 then col1
        else 
          case
            when col2 = 1 and not null in (col4, col5) then 2
            when col2 = 1 and not null in (col3, col4) and col5 is null then 1
            when col2 = 2 and not null in (col3, col4, col5) then 3
            else null -- col2 not in (1,2)
          end
      end
    

    存储它们会是这样的:

    insert into my_table (mycolumn)
    
    select
      case
        when col1 >= 3 then col1
        else
          case
            when col2 = 1 and not null in (col4, col5) then 2
            when col2 = 1 and not null in (col3, col4) and col5 is null then 1
            when col2 = 2 and not null in (col3, col4, col5) then 3
            else null -- col2 not in (1,2)
          end
      end
    

    【讨论】:

      【解决方案3】:

      使用 CASE 表达式时要小心,因为它按顺序工作并在第一个 TRUE 结果时退出。因此,条件的排序至关重要。

      WITH
          tbl (C1, C2, C3, C4, C5) AS
              (
                  Select 1, 1, 3, 4, 5 From Dual Union All
                  Select 1, 1, 3, 3, 5 From Dual Union All
                  Select 1, 1, 3, 3, Null From Dual Union All
                  Select 2, 2, 3, 3, 5 From Dual Union All
                  Select 2, 2, 3, Null, Null From Dual Union All
                  Select 2, 2, Null, Null, Null From Dual Union All
                  Select 4, 2, 3, 3, 5 From Dual
              )
      Select t.*,
          CASE 
              WHEN C1 >= 3 THEN C1
          ELSE
              CASE 
                  WHEN C2 = 1 And     C3 Is Not Null And      C4 Is Not Null And      C5 Is Not Null THEN 2
                  WHEN C2 = 1 And     C3 Is Null And          C4 Is Not Null And      C5 Is Not Null THEN 2
                  WHEN C2 = 1 And     C3 Is Not Null And      C4 Is Not Null And      C5 Is Null THEN 1
                  WHEN C2 = 1 And     C3 Is Not Null And      C4 Is Null And          C5 Is Null THEN 1
                  --
                  WHEN C2 = 2 And     C3 Is Not Null And      C4 Is Not Null And      C5 Is Not Null THEN 3
                  WHEN C2 = 2 And     C3 Is Null And          C4 Is Not Null And      C5 Is Not Null THEN 3
                  WHEN C2 = 2 And     C3 Is Not Null And      C4 Is Not Null And      C5 Is Null THEN 2
                  WHEN C2 = 2 And     C3 Is Not Null And      C4 Is Null And         C5 Is Null THEN 2
              ELSE 
                  -1
              END
          END "RESULT"
      From tbl t
      Order By 1
      
      R e s u l t :
              C1         C2         C3         C4         C5     RESULT
      ---------- ---------- ---------- ---------- ---------- ----------
               1          1          3          4          5          2 
               1          1          3          3          5          2 
               1          1          3          3                     1 
               2          2          3          3          5          3 
               2          2          3                                2 
               2          2                                          -1 
               4          2          3          3          5          4
      

      结果-1表示没有匹配条件

      【讨论】:

        猜你喜欢
        • 2020-12-29
        • 2018-09-26
        • 1970-01-01
        • 1970-01-01
        • 2013-12-17
        • 2022-06-28
        • 1970-01-01
        • 1970-01-01
        • 2017-09-25
        相关资源
        最近更新 更多