在列中具有共享值的两行之间交换值

时间:2015-01-07 07:18:19

标签: sql sql-server rdbms

我在SQL Server表中有数据如下:

Name         D/C       Code

Expenses     Debit     10000 
Expenses     Credit    20000
Transport    Debit     50000 
Transport    Credit    60000

我想像这样交换代码列值:

Name       D/C       Code

Expenses   Debit     20000
Expenses   Credit    10000
Transport  Debit     60000
Transport  Credit    50000

有没有办法使用SQL UPDATE命令执行此操作?

1 个答案:

答案 0 :(得分:0)

如果您有标识栏请尝试此

update t1 
set
t1.code=t2.code
from your_table as t1 inner join your_table as t2
on t1.name=t2.name
where t1.[D/c]='Debit' and t2.[D/c]='Credit' and t1.id_col=t2.id_col-1