Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用上一个单元格sql中的另一个值更新值_Sql_Sql Server - Fatal编程技术网

使用上一个单元格sql中的另一个值更新值

使用上一个单元格sql中的另一个值更新值,sql,sql-server,Sql,Sql Server,我有一个需要解决的问题 1查找与1900-01-01相同的任何日期 2将其替换为上一个单元格中的值 您可以使用lag(): 编辑: 如果要更新该值,请使用可更新的CTE: with toupdate as ( select t.*, lag(date) over (order by id) as prev_date from t ) update toupdate set date = prev_date where date = '1900-0

我有一个需要解决的问题 1查找与1900-01-01相同的任何日期 2将其替换为上一个单元格中的值

您可以使用
lag()

编辑:

如果要更新该值,请使用可更新的CTE:

with toupdate as (
      select t.*, lag(date) over (order by id) as prev_date
      from t
     )
update toupdate
    set date = prev_date
    where date = '1900-01-01';

除了它们在屏幕上的外观,数据库表与电子表格没有太多共同之处。数据库将数据存储在无序的集合中,因此在您通过查询强制执行顺序之前,“previous”没有任何实际意义,因此建议答案中的
orderby
子句。此外,表没有“单元格”,它们有行和列。改变你的思维方式可以更容易地概念化问题。
with toupdate as (
      select t.*, lag(date) over (order by id) as prev_date
      from t
     )
update toupdate
    set date = prev_date
    where date = '1900-01-01';