如何将另一行的值插入到列中 - SQL 服务器
How to insert a value from another row into a column - SQL Server
我正在为一个项目SQL工作,我需要根据一些规则更新Soh_Wh_A和Soh_Wh_B。
这是table_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
我想填充列 Wh_A 和 Wh_B。例如,让我们处理第一行,Wh_A 应该具有与列 StockOnHand 相同的值,因为该行属于仓库“A”。使用 update case when 语句很容易做到这一点。
对我来说困难的是用第二行的 StockOnHand 列填充 Wh_B 列。
最后的table应该是这样的
| 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
这是我到目前为止所做的...
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
您可以使用 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
使用可更新的 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
我正在为一个项目SQL工作,我需要根据一些规则更新Soh_Wh_A和Soh_Wh_B。
这是table_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
我想填充列 Wh_A 和 Wh_B。例如,让我们处理第一行,Wh_A 应该具有与列 StockOnHand 相同的值,因为该行属于仓库“A”。使用 update case when 语句很容易做到这一点。
对我来说困难的是用第二行的 StockOnHand 列填充 Wh_B 列。
最后的table应该是这样的
| 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
这是我到目前为止所做的...
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
您可以使用 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
使用可更新的 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