仅选择最新金额,如果为null,则在此之前。
表一
customer|amount|date
001|2 |20201101
001|null|20201102
001|3 |20201103
002|8.9 |20201101
002|7 |20201008
002|null|20201106
结果
001|null|20201101
001|null|20201102
001|3 |20201103
002|null|20201101
002|null|20201008
002|7 |20201106
金额数据应按日期获取最新数据,其他记录将为null,如果金额为最新日期为null,则应获取先前的非null值。我目前的尝试:
select top 1 [amount]
from table
where [amount] is not null
order by date desc
如果要将除最近值以外的所有值都设置为NULL
:
select customer_code, date,
(case when seqnum = 1 then amount end) as amount
from (select t.*,
row_number() over (partition by customer_code order by (amount is not null) desc, date desc) as seqnum
from table t
) t
where customer_code = '001'
order by date desc
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句