繁体   English   中英

SQL - 根据其他列的值重命名一列

[英]SQL - Rename one column based on values from other columns

假设我们正在查看从以下城市出发的公共汽车的出发日期:

城市 出发日期 货车号码
柏林 2022-05-01 001
伦敦 2022-05-22 020
柏林 2022-05-22 030
伦敦 2022-05-08 090

如果出发日期相同,我需要重命名“城市”列。 这应该是我的 output:

城市 出发日期 货车号码
柏林 2022-05-01 001
中央 2022-05-22 020
中央 2022-05-22 030
伦敦 2022-05-08 090

在这种情况下,当伦敦和柏林具有相同的 department_date 时,我们将它们重命名为“Central”

预先感谢您的帮助!

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
  )

暂无
暂无

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

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