简体   繁体   中英

SQL Nested CASE WHEN Statement

How do I change this SELECT statement to create an adj_b2 column that depends on the same condition as adj_a2 and an adj_b3 that depends on a different condition?

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

You just use three separate CASE statements:

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.*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM