如何遍历表并更新多行

问题描述 投票:0回答:1

嗨基于下表我有2个表,其中名称是销售记录历史,另一个是销售信息。销售信息表上的基础josh已售出100我如何更新销售记录历史表

场景:

josh包一个已经售出50怎么可以把100个卖的信息添加到包装1 50加上销售,因为最大上限是100和销售的余额添加到josh的包2

table - sell Record history		
name	package	max cap	sold
Josh	1	100	0
Jack	1	100	0
Josh	2	100	0
Austin	1	100	0

table -sell info	
name	sold
Josh	150
Jack	0
Austin	0

预期产量将是:

name    |package|max cap    |sold
Josh    |1      |100        |100
Jack    |1      |100        |0
Josh    |2      |100        |50
Austin  |1      |100        |0
sql sql-server loops
1个回答
2
投票

无法在一个简单的查询中执行此操作。下面的查询我使用递归CTE。

假设:包是数字的,从1开始,递增1而没有间隙

-- Create sample table
declare @sell_record table
(
    name        varchar(6),
    package     int,
    max_cap     int,
    sold        int
)

declare @sell_info table
(
    name        varchar(6),
    sold        int
)

-- Insert sample data
insert into @sell_record select 'Josh', 1, 100, 0
insert into @sell_record select 'Jack', 1, 100, 0
insert into @sell_record select 'Josh', 2, 100, 0
insert into @sell_record select 'Austin', 1, 100, 0

insert into @sell_info select 'Josh', 150
insert into @sell_info select 'Jack', 0
insert into @sell_info select 'Austin', 0

-- The query. rcte is recursive cte
; with rcte as
(
    -- anchor member. Starts with package 1
    select  i.name, r.package,
            -- allocation of sold_qty to package
            alloc    = case when i.sold >= r.max_cap - r.sold 
                            then r.max_cap - r.sold 
                            else i.sold
                            end,
            -- balance of the sold qty after allocated
            bal_sold = case when i.sold >= r.max_cap - r.sold 
                            then i.sold - r.max_cap - r.sold 
                            else 0
                            end
    from    @sell_info i
            inner join @sell_record r   on  i.name  = r.name 
    where   r.package   = 1

    union all

    -- recursive member
    select  c.name, r.package,
            -- allocation of sold_qty to package
            alloc   = case  when c.bal_sold >= r.max_cap - r.sold 
                            then r.max_cap - r.sold 
                            else c.bal_sold
                            end,
            -- balance of the sold qty after allocated
            bal_sold = case when c.bal_sold >= r.max_cap - r.sold 
                            then c.bal_sold - r.max_cap - r.sold 
                            else 0
                            end
    from    rcte c
            inner join @sell_record r   on  c.name      = r.name 
                                        and c.package   = r.package - 1
)
-- Update back sell_record
update  r
set    sold = c.alloc
from    rcte c
        inner join @sell_record r   on  c.name  = r.name
                                    and c.package   = r.package

-- show the result
select  *
from    @sell_record

/*
name    package max_cap sold
Josh    1   100 100
Jack    1   100 0
Josh    2   100 50
Austin  1   100 0
*/
© www.soinside.com 2019 - 2024. All rights reserved.