从所有列中获取 Last NOT NULL 值
问题描述
我有一种情况,我试图从所有列中获取最后一个非空值.这是一些用于演示目的的数据.
I have a situation where I am trying to get last not null value from all the columns. here is some data to for demo puposes.
测试数据
我已经建立了一个返回所需结果的查询.但想知道是否有更高效的方法来做到这一点.由于这种方式我每次都在查询整个表,我希望找到一种更有效的方法来获得这些结果.
I have built a query which returns the required results. But was wondering if there is a more performance efficient way of doing this. Since this way I am quering the whole table each time I hope to find a more efficient way of getting these results.
我的查询
结果集
它返回正确的结果.但我确信可以通过 更简单/高效的查询 来完成.任何帮助或指针都非常受欢迎
It returns the correct results. but I am sure it can be done with a bit more simpler/efficient query . Any help or pointer is much appericiated
推荐答案
非常简单UNPIVOT
操作和适当的 ROW_NUMBER
选择最近的值.UNPIVOT
自动消除 NULL
值:
Pretty much a straightforward UNPIVOT
operation and an appropriate ROW_NUMBER
to select the most recent value. The UNPIVOT
automatically eliminates NULL
values:
结果:
这篇关于从所有列中获取 Last NOT NULL 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!