简体   繁体   English

SQL 服务器 - 根据其他列的值更新一列

[英]SQL server - Update one column based on the values of other columns

Say there are five columns in the table and I'd like to update the column E based on the value of column A,B,C,D.假设表中有五列,我想根据列 A、B、C、D 的值更新列 E。 If the corresponding value is Y, then write the column name as part of the value in column E. The expected result is shown in the second image.如果对应的值为 Y,则将列名作为 E 列中值的一部分写入。预期结果显示在第二张图像中。

Originally, the data in the table looks like this原来表中的数据是这样的

在此处输入图像描述

Expected result:预期结果:

在此处输入图像描述

If you number of columns is fixed, then you can use a CASE statement.如果您的列数是固定的,那么您可以使用CASE语句。

Sample data样本数据

create table data
(
  A nvarchar(1),
  B nvarchar(1),
  C nvarchar(1),
  D nvarchar(1),
  E nvarchar(10)
);

insert into data (A, B, C, D) values
('Y', 'N', 'Y', 'Y'),
('N', 'N', 'N', 'Y'),
('N', 'Y', 'Y', 'N'),
('Y', 'Y', 'Y', 'N');

Solution解决方案

update d
set d.E = substring(
          case d.A when 'Y' then ',A' else '' end
        + case d.B when 'Y' then ',B' else '' end
        + case d.C when 'Y' then ',C' else '' end
        + case d.D when 'Y' then ',D' else '' end,
         2, 100)
from data d;

Result结果

select * from data;

A   B   C   D   E
--- --- --- --- -------
Y   N   Y   Y   A,C,D
N   N   N   Y   D
N   Y   Y   N   B,C
Y   Y   Y   N   A,B,C

SQL Fiddle SQL 小提琴

SQL Server now supports CONCAT_WS() , which I would recommend: SQL 服务器现在支持CONCAT_WS() ,我建议:

update t
    set E = concat_ws(',',
                      case d.A when 'Y' then 'A' end,
                      case d.B when 'Y' then 'B' end,
                      case d.C when 'Y' then 'C' end,
                      case d.D when 'Y' then 'D' end
                     );

However, it seems silly to store this information when it can be calculated -- and always up-to-date:但是,在可以计算时存储这些信息似乎很愚蠢 - 并且始终是最新的:

alter table t add E as (concat_ws(',',
                                  case d.A when 'Y' then 'A' end,
                                  case d.B when 'Y' then 'B' end,
                                  case d.C when 'Y' then 'C' end,
                                  case d.D when 'Y' then 'D' end
                                 )
                        );

Computed columns are calculated when they are queried.计算列在查询时计算。 They are always up-to-date and never have to be updated.它们始终是最新的,无需更新。

update table_name set E='A,C,D' where A='Y' and B='N' and C='Y' and D='Y'

If you set id column for your table it is easy to update如果您为表设置 id 列,则很容易更新

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

相关问题 如何基于另一列SQL Server 2014中的数据更新一列上的值 - How to update the values on one column based on the data from other column SQL Server 2014 SQL查询根据同一表中其他列的值更新列 - SQL Query to update a column based on the values of other columns in the same table SQL Server:根据其他2列中的条件更新boolean列 - SQL Server : update boolean column based on conditions from 2 other columns 根据 SQL Server 中其他表中的值更新多个列 - Update multiple columns based on values from other table in SQL Server SQL - 根据其他列的值重命名一列 - SQL - Rename one column based on values from other columns 根据 SQL Server 中其他列的值在列内进行划分 - Dividing within a column based on values of other columns in SQL Server 如何根据其他列的值更新列 - How to update a column based on values of other columns SQL Server:基于一个列进行分组,并根据另一列获取列 - SQL Server: Group By based on one column and get columns based on other column 如何根据 SQL Server 中的另一列将一列值转换为两个不同的列值? - How to convert one column values to two different column values based on other column in SQL Server? SQL Server 2000-根据其他3列的值从4列表返回单个列值 - SQL Server 2000 - Returning a single column value from a 4 column table based on the values of the other 3 columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM