简体   繁体   中英

SQL - Rename one column based on values from other columns

Let's say we're looking at the date of departure of a bus from the following cities:

City Departure_Date Truck Number
Berlin 2022-05-01 001
London 2022-05-22 020
Berlin 2022-05-22 030
London 2022-05-08 090

I need to rename the column 'City' if the departure_date is the same. This should be my output:

City Departure_Date Truck Number
Berlin 2022-05-01 001
Central 2022-05-22 020
Central 2022-05-22 030
London 2022-05-08 090

In this case we rename London and Berlin for 'Central' when they have the same departure_date

Thank you in advance for your help!

update t set t.City = 'Central'
from dbo.yourTable t
where exists (
  select
    1
  from dbo.yourTable tt 
  where tt.Departure_Date = t.Departure_Date
    and tt.id != t.id
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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