当前位置:数据库 > SQLServer >>

我在sqlserver中有一个表

我现在一个表有有些数据,其中有些数据为空的,现在要把这些为空的数据用它前面的非空的数据补充,如果前面的也为空,则继续前推,请问各位大神们 该如何实现? 数据截取如下: ID DTIME GB ---------- ---------- ---------- 1 201001 3 1 201002 2 1 201003 1 201004 1 201005 1 2 201001 2 2 201002 3 2 201003
追问:用这个方法,如果连续有很多为空值的那种情况,则只补充了第一条为空的,后面为空的那些行就没有补充了
答案:试试如下写法:

update table 
set GB = (select GB from table b where table.ID = b.ID and DTIME = (select max(DTIME) from table c where b.ID = C.ID and C.DTIME < table.DTIME)
where GB is null
其他:select id,dtime,
case when gb is null then dtime else gb end gb 
from table
如果你要继续往前推,写循环吧 update 表 set gb=case when gb=null and dtimeis not null then dtime when gb=null and dtimeis =null then id end  where gb=null g update tb set gb=g
from (select dtime,gb as g from(select dtime,gb ,sort=row_number() over (order by id,dtime desc))x where sort=1)c
where gb is null  and c.id =id
 
还真有点糊涂  试下 吧 

上一个:sqlserver 表1 数据导入到表2
下一个:SQLSERVER 2005 执行全文查询失败。"服务没有运行。",怎么回事

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,