在 SQL 中将一行值移动到另一行 [英] Move one row value to another in SQL

查看:57
本文介绍了在 SQL 中将一行值移动到另一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个这样的临时表

I currently have a temporary table like so

DBName API50 CounterValue
NULL    NULL   1
test1   34.5   NULL
NULL    NULL   2
test1   38.5   NULL

我想要一个脚本来制作我的临时表

I want a script which will make my temporary table as below

DBName API50 CounterValue
test1   34.5   1
test1   38.5   2

推荐答案

如果你的表有主键,并且你总是想将 CounterValue 字段与表中的下一个字段关联,你可以做一个自联接:

If your table has a primary key, and you always want to associate the CounterValue field with the next field in the table, you can do a self-join:

SELECT t1.DBName, t1.API50, t2.CounterValue
FROM MyTable t1 INNER JOIN MyTable t2 ON t1.PrimaryKey -1 = t2.PrimaryKey
WHERE t1.DBName IS NOT NULL

这篇关于在 SQL 中将一行值移动到另一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆