简体   繁体   English

如何将另一行中的值插入列中 - SQL 服务器

[英]How to insert a value from another row into a column - SQL Server

I'm working on SQL for a project, I need to update Soh_Wh_A and Soh_Wh_B based on some rules.我正在为一个项目开发 SQL,我需要根据一些规则更新 Soh_Wh_A 和 Soh_Wh_B。

This is table_A:这是表_A:

| Code  | Warehouse | StockOnHand | Wh_A     | Wh_B 
----------------------------------------------------
| 001   | A         | 10          | NULL     | NULL     
| 001   | B         | 20          | NULL     | NULL     
| 003   | A         | 30          | NULL     | NULL     
| 033   | B         | 40          | NULL     | NULL     

I want to populate columns Wh_A and Wh_B.我想填充列 Wh_A 和 Wh_B。 For example, lets work on the first row, Wh_A should have the same value of the column StockOnHand as this row belongs to warehouse "A".例如,让我们处理第一行,Wh_A 应该与 StockOnHand 列的值相同,因为该行属于仓库“A”。 That is easy to do using an update case when statement.使用 update case when 语句很容易做到这一点。

What is difficult for me is to populate the column Wh_B with the column StockOnHand from the second row.对我来说困难的是用第二行的 StockOnHand 列填充 Wh_B 列。

The table should be like this at the end.桌子最后应该是这样的。

| Code  | Warehouse | StockOnHand | Wh_A     | Wh_B 
----------------------------------------------------
| 001   | A         | 10          | 10       | 20
| 001   | B         | 20          | 10       | 20
| 003   | A         | 30          | 30       | 40     
| 033   | B         | 40          | 30       | 40     

This is what I have done so far...这是我到目前为止所做的......

update Table_A set
Wh_A = (case 
        when warehouse = 'A' then stockOnHand
        when warehouse = 'B' then ... end)
Wh_B = (case 
        when warehouse = 'A' then stockOnHand
        when warehouse = 'B' then ... end)



declare @Table table (
    Code        char(3) not null,
    Warehouse   char(1) not null,
    StockOnHand int     not null,
    Wh_A        int         null,
    Wh_B        int         null,

    primary key (Code, Warehouse)
);

insert into @Table
    (Code, Warehouse, StockOnHand)
values 
    ('001', 'A', 10), 
    ('001', 'B', 20), 
    ('003', 'A', 30), 
    ('003', 'B', 40);

update t set
    Wh_A = (select top 1 StockOnHand from @Table a where a.Warehouse = 'A' and a.Code = t.Code),
    Wh_B = (select top 1 StockOnHand from @Table b where b.Warehouse = 'B' and b.Code = t.Code)
from 
    @Table t;


select * from @Table

You can use window functions:您可以使用 window 函数:

select 
    code,
    warehouse,
    stockOnHand,
    max(case when warehouse = 'A' then stockOnHand end)
        over(partition by code) wh_a,
    max(case when warehouse = 'B' then stockOnHand end)
        over(partition by code) wh_b
from table_a

It is easy to turn this to an update query using an updateable cte:使用可更新的 cte 很容易将其转换为update查询:

with cte as (
    select 
        wh_a,
        wh_b
        max(case when warehouse = 'A' then stockOnHand end)
            over(partition by code) new_wh_a,
        max(case when warehouse = 'B' then stockOnHand end)
            over(partition by code) new_wh_b
    from table_a
)
update cte set wh_a = new_wh_a, wh_b = new_wh_b

暂无
暂无

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

相关问题 如何通过从 SQL Server 中的另一个表中选择列名和值将行插入表中 - How to insert row into a table by select column name and value from another table in SQL Server SQL在一行中选择第一个值,然后将该值插入另一列 - SQL select first value in a row then insert that value into another column 如果另一列在SQL Server中相同,则获取具有最小列值的行 - Getting the row with the smallest column value if another column is the same in SQL Server 使用SQL Server另一列中的行值计数更新列 - Update column with count of row value in another column in SQL Server SQL Server:确定第一行值出现在列中并插入表中 - SQL Server : identify first row value appears in column and insert into table SQL Server:排除表中的行如果有另一行具有相同的col值和特定的二级列值 - SQL Server : excluding rows from a table IF there is another row with the same col value and a particular secondary column value 如何使用来自同一表中另一特定行的列值更新sql表中每一行的列 - How to update a column of every row in a sql table with the value of that column from another specific row in the same table 如何将一个表列行分配给 SQL Server 中的另一个表列值 - How to assign one table column row to another table column value in SQL Server 如何从第2行中减去第1行并将结果插入SQL Server中的第3行中? - How to subtract row 1 from row 2 and insert the result in row 3 in SQL Server? Sql Server触发器将值从新行插入另一个表 - Sql Server trigger insert values from new row into another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM