简体   繁体   English

SQL:通过联接将数据从一列复制到同一表中的另一列,然后删除

[英]SQL: To Copy data from one column to another in same table by joining and then delete

I 'm getting errors when I try to copy data from 1 column to another, since there are 2 rows with almost same data but diff unique IDs. 尝试将数据从1列复制到另一列时出现错误,因为有2行数据几乎相同,但具有唯一ID。 I used the select statement which works fine to select the specific dates to be copied to the del_date column from train_date but the condition in where statement. 我使用了select语句,该语句可以很好地选择要从train_date复制到del_date列的特定日期,但要选择where语句中的条件。 How can I copy the data to the del date and then delete the train_date rows. 如何将数据复制到预定日期,然后删除train_date行。 Thanks 谢谢

update dbo.Comp d
  set d.del_date =  t.train_date 
  (
   SELECT d.unique_id as delUnique
      ,t.unique_id  as TraUnique
      ,d.del_date as delDelDate
      ,t.del_date as traDelDate
      ,d.train_date as delTrainDt
      ,t.train_date as traTrainDt

  FROM dbo.Comp d 
  inner join dbo.Comp t on d.g_id = t.g_id 
  where t.del_date = d.train_date
  and t.unique_id <> d.unique_id
  order by d.og_id, d.g_id

Try this, 尝试这个,

update d
set d.del_date =  t.train_date 
FROM dbo.Comp d 
inner join dbo.Comp t on d.g_id = t.g_id 
where t.del_date = d.train_date
and t.unique_id <> d.unique_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM