1

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. 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. How can I copy the data to the del date and then delete the train_date rows. 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
2
  • What DBMS are you using (MySQL, SQL Server)?
    – PinnyM
    Feb 8, 2013 at 15:55
  • I think I know waht you mean but a bit of example data wouldn't go amiss. Feb 8, 2013 at 15:55

1 Answer 1

1

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
10
  • Thanks, When I use the upper statement I get the following error:
    – SAL
    Feb 8, 2013 at 16:09
  • Msg 156, Level 15, State 1, Line 7 Incorrect syntax near the keyword 'order'.
    – SAL
    Feb 8, 2013 at 16:09
  • And if I take off the order by, then it gives me the following error: Msg 209, Level 16, State 1, Line 6 Ambiguous column name 'unique_id'. Msg 209, Level 16, State 1, Line 6 Ambiguous column name 'unique_id'.
    – SAL
    Feb 8, 2013 at 16:10
  • Remove the order by, i alter the query
    – Rednaxel
    Feb 8, 2013 at 16:12
  • 1
    Thanks, this is the first time I'm using a blod, can you please guide me how to set it as correct?
    – SAL
    Feb 8, 2013 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.