简体   繁体   English

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

[英]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 2022-05-01 001 001
London伦敦 2022-05-22 2022-05-22 020 020
Berlin柏林 2022-05-22 2022-05-22 030 030
London伦敦 2022-05-08 2022-05-08 090 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 2022-05-01 001 001
Central中央 2022-05-22 2022-05-22 020 020
Central中央 2022-05-22 2022-05-22 030 030
London伦敦 2022-05-08 2022-05-08 090 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.

相关问题 如何添加一个列,该列根据另一列的条件对其他列的值进行字符串加法 - How to add a column which does a string addition of values from other column based on condition from another column 将列的值与其他列进行比较,并根据条件从第三列中选择值 sql - Compare column's value with other column and base on condition choose value from third column sql 根据条件从一列中提取数据并存储在另一列中 - Extracting data from one column and storing in another based on a condition SQL CountIf 根据平均值从另一列满足条件 - SQL CountIf a condition is met from another column based on average 根据 redshift 中另一列的值创建列 - Create column based on values on another column in redshift Select 其他表作为 BigQuery 中基于日期时间的列 - Select other table as a column based on datetime in BigQuery SQL - 根据 2 个值删除重复项 - SQL - Remove Duplicates based on 2 values 来自相似列的连接值 - Concat values from similar columns 如何通过在 bigquery sql 中进行分组字符串比较来返回同一列中字符串值的差异? - How to return difference in string values from the same column by doing a grouped string comparison in bigquery sql? 如何对 SQL 中共享相同列名的 2 个单独表中的值求和 - How to SUM values from 2 separate tables that share the same column name in SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM