How to merge two data columns and show it as one column?



  • In sum1 the data has stopped coming and is replaced with sum2. I want the previous data from sum1 and merge with the new value of sum2. Below is the query but not working.

    SELECT 
    time AS "time",
    CASE WHEN (data->>'sum1') in (data->>'sum1')::numeric END as "" or
    CASE WHEN (data->>'sum2')='' 
    THEN NULL ELSE (data->>'sum2')::numeric END as ""
    FROM data WHERE  id = 68 AND
    time > $__timeFrom()AND  time < $__timeTo() ORDER BY 1,2
    


  • I think you want something like this:

    SELECT time AS "time",
           CASE 
              WHEN data ? 'sum1' then (data->>'sum1')::numeric 
              ELSE (data->>'sum2')::numeric 
           END as "sum"
    FROM data 
    WHERE id = 68 
      AND time > $__timeFrom()
      AND time < $__timeTo() 
    ORDER BY 1,2
    

    If the key sum1 exists in the data column, then use the value from that, else use the value from the key sum2 .

    Another option is to use coalesce() to pick the first non-null value:

    coalesce((data->>'sum1')::numeric, (data->>'sum2')::numeric) as "sum"
    

    Both solutions assume that there will never be a value where both keys exists.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2