新列取决于sql条件

新列取决于sql条件,sql,conditional-statements,Sql,Conditional Statements,假设我有一个有两列的表,如下所示: 如果ID的类型为信用卡,我想添加另一列。如何做到这一点?如果我理解正确,那么您可以使用case表达式添加另一列,如下所示 select ID, Type, case when Type = 'Credit Card' then yourValue end as yourColumnName from yourTable 如果我理解正确,它将使用窗口功能: select t.*, max(case when type

假设我有一个有两列的表,如下所示:


如果ID的类型为
信用卡
,我想添加另一列。如何做到这一点?

如果我理解正确,那么您可以使用
case
表达式添加另一列,如下所示

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

如果我理解正确,它将使用窗口功能:

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

表的所有行具有相同的列数。你想干什么?请告诉我们你期望的结果。