Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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 - Fatal编程技术网

Sql 通过多次将数据从一行移动到另一行来更新表

Sql 通过多次将数据从一行移动到另一行来更新表,sql,Sql,有一个sql表,我需要获取第2行,并将第4列和第5列中的数据移动到第1行。第4列和第5列中的值是动态的(即每次不相同) 背景在桌子上。每8行分组到另一个表中的另一个实体(但此任务不需要另一个表) 比如说 action_id = 839283 col 4 = space col 5 = space action_id = 839284 col 4 = SMS col 5 = L1 我需要将SMS&L1移动到上面的行,并将action_id=839284的行清空 这将重复多次。 我想在临时表中创建

有一个sql表,我需要获取第2行,并将第4列和第5列中的数据移动到第1行。第4列和第5列中的值是动态的(即每次不相同)

背景在桌子上。每8行分组到另一个表中的另一个实体(但此任务不需要另一个表) 比如说

action_id = 839283 col 4 = space col 5 = space
action_id = 839284 col 4 = SMS col 5 = L1
我需要将SMS&L1移动到上面的行,并将action_id=839284的行清空 这将重复多次。 我想在临时表中创建一个select来获取需要更改的行(使用另外两个表作为链接),但我不知道如何动态地将数据从一行移动到另一行

  • sql server
  • action_id值始终是分开的
3个STPE:

select action_id as To_Empty
into #EmptyMe
from MyTable
where col4 is not null

with CTE as
(
select action_id,
       col4, 
       col5, 
       lag(col4) over (order by action_id) as n_col4, 
       lag(col5) over (order by action_id) as n_col5
from MyTable
where col4 is not null
)
update CTE
set col4 = n_col4, col5 = n_col5 

update MyTable
set col4 = null, col5 = null
where exists (select 1 from #EmptyMe where action_id = To_Empty)

编辑您的问题并(1)提供样本数据和所需结果;(2) 你正在使用的数据库的标记。我不明白。毕竟,您似乎只是想用具有最大action_id的记录的值更新具有最小action_id的记录,并将所有其他记录中的列设置为null。这就是你想要的吗?这是关于8行一组的吗?哪个范围?839280-839287? 还是839281-839288?或839282-839289。。。不知何故,包括数据模型在内的整个任务似乎很奇怪。也许你想在这两方面都详细说明并得到帮助。你用的是哪一种?博士后?神谕