求教SQL 查询语句
sql吧
全部回复
仅看楼主
level 1
hc72szh 楼主
SQL 数据库中有一表格。
Date Value1 Value2
2020-11-25 500 200
2020-11-24 Null 200
2020-11-23 Null 250
2020-11-22 300 Null
2020-11-21 200 320
。。。
查询所得:
Date Value1 Value2
2020-11-25 500 200
2020-11-24 300 200
2020-11-23 300 250
2020-11-22 300 320
2020-11-21 200 300
。。。
描述: 如果某列的字段在该日期为空值,查询时结果显示为之前日期最后一天的非空值。
2020年11月25日 08点11分 1
level 8
MSSQL语法。如果是其他数据库请自行修改
select Date,
isnull(Value1,(select top 1 Value1 from 表 where Date<a.Date and Value1 is not null order by Date desc)),
isnull(Value2,(select top 1 Value2 from 表 where Date<a.Date and Value2 is not null order by Date desc)) from
表 a
order by Date desc
2020年11月26日 01点11分 2
1