根据另一列的值选择特定列

2021-09-10 00:00:00 sql tsql sql-server

我有一张这样的桌子

ID | Type | Val0 | Val1
1  |  0   |  A   | NULL
2  |  1   | NULL |  B

类型为0时需要选择Val0,类型为1时选择Val1,类型为N时选择ValN...

I need to select Val0 when the type is 0, and Val1 when the type is 1, and ValN when type is N...

我该怎么做?

推荐答案

SELECT CASE
          WHEN Type = 0 THEN Val0
          WHEN Type = 1 Then Val1
          .
          .
          WHEN Type = N Then ValN
       END 
  FROM tbl

相关文章