[英]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:
这应该是我的 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在这种情况下,当伦敦和柏林具有相同的 department_date 时,我们将它们重命名为“Central”
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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.