简体   繁体   中英

New column depending on conditions sql

Let's say that I have a table with 2 columns, like this:

桌子

And I want to add another column if the ID has the type credit card . How to do that?

If I get you correctly then you can use case expressions to add another column as following

select
  ID,
  Type,
  case 
    when Type = 'Credit Card' then yourValue
  end as yourColumnName
from yourTable

If I understand correctly, it would use window functions:

select t.*,
       max(case when type = 'Credit Card' then 1 else 0 end) over (partition by id) as has_credit_card
from t;

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