用于检索最后填充的列值的SQL语句作为结果

时间:2017-04-03 09:28:38

标签: sql sql-server sql-server-2008 sql-server-2005 sql-server-2012

使用下面的sql语句从表字段中获取最后一个填充列但不能使其工作。

select
  iif(isnull(AppBy11,true),
  iif(isnull(AppBy10,true),
  iif(isnull(AppBy9,true),
  iif(isnull(AppBy8,true),
  iif(isnull(AppBy7,true),
  iif(isnull(AppBy6,true),
  iif(isnull(AppBy5,true),
  iif(isnull(AppBy4,true),
  iif(isnull(AppBy3,true),
  iif(isnull(AppBy2,true), "", AppBy2), AppBy3), AppBy4), AppBy5), AppBy6), AppBy7), AppBy8), AppBy9, AppBy10), AppBy11) as Result
from entry

enter image description here

有没有其他方法可以达到预期效果的闪电速度?我将为600万个数据运行此查询。

1 个答案:

答案 0 :(得分:4)

利用COALESCE (Transact-SQL)。它将在列表中返回第一个NOT Null列值。

SELECT COALESCE(AppBy11, AppBy10,AppBy9,AppBy8,AppBy7,AppBy6,AppBy5,AppBy4,AppBy3,AppBy2,AppBy1) as Result 
FROM entry