SQL Nested CASE WHEN语句

时间:2015-07-23 23:30:48

标签: mysql sql

如何更改此SELECT语句以创建adj_b2列,该列取决于与adj_a2相同的条件和取决于不同条件的adj_b3?< / p>

 SELECT CASE
        WHEN c.1 > 5
            THEN COALESCE(a.2,0)*2 
            ELSE COALESCE(a.2,0)                
    END as adj_a2, a.*, b.*, c.*

1 个答案:

答案 0 :(得分:0)

您只需使用三个单独的CASE语句:

SELECT
  CASE
    WHEN c.1 > 5
      THEN COALESCE(a.2,0)*2 
    ELSE COALESCE(a.2,0)                
  END as adj_a2,
  CASE
    WHEN c.1 > 5
      THEN COALESCE(b.2,0)*2 
    ELSE COALESCE(b.2,0)                
  END as adj_b2,
  CASE
    WHEN c.2 < 42
      THEN COALESCE(a.2,0)*c.3 
    ELSE COALESCE(b.2,0)*c.4                
  END as adj_b3,
  a.*,
  b.*,
  c.*