Home > Mobile >  inserting values in to temporary column from the select query
inserting values in to temporary column from the select query

Time:11-24

I have a select query as below and I wanted to insert text in to temp (ex: ex, ab , cbhk) of row of city and respective code and the remaining should be empty how to do in the best way. Please suggest

    SELECT DISTINCT
    city,
    code,
    ' ' as Temp
    FROM table t1
    where value='notequal'

Will give output

city code Temp
bang codea
bang codeb
bang3 codec
afghan coded
afghan codee

Needed output

city code Temp
bang codea
bang codeb ex
bang3 codec
afghan coded ab
afghan codee cbhk

CodePudding user response:

You just need case condition

 SELECT DISTINCT
    city,
    code,
    Case
    When city='bang' and code='codeb' then 'ex'
    When city='afghan' and code='coded' then 'ab'
     Else ' ' as Temp
     END
   where…. ```

  • Related