0

1つの図を複数の行に分割できるクエリを作成しようとしています。

たとえば、発注書xには15個のアイテムが割り当てられている場合があります。送料は、たとえば9.95です。9.95の1/15を計算してから、在庫の原価を送料の1/15に更新するにはどうすればよいですか?

したがって、アイテムの原価は4.50から5.16(4.50 + 0.66)に増加します。

4

2 に答える 2

2

SQLServerのソリューションは次のとおりです。

update  ol
set     price = price + 5.0 / ol.LineCount
from    [Order] o
join    (
        select  *
        ,       count(*) over () as LineCount
        from    OrderLine
        ) ol
on      o.ID = ol.OrderID
where   o.OrderNr = 'Ord1';

SQLFiddleでの実例。

別のDBMSを使用している場合は、投稿を更新してください。

于 2012-11-08T15:30:53.320 に答える
0

商品表を作成し、商品の送料を更新しました。それがあなたが探しているものであることを願っています。

INSERT INTO [TravelAgentDB].[dbo].[Product]
           ([ID]
           ,[Name]
           ,[Price]
           ,[shippingPrice])
     VALUES
          (1,'Nexus 7' , 250 , 20),
          (2,'Nexus 7 case' , 50 , 20),
          (3,'Nexus 7 headphone' , 20 , 20)
GO


select * from product
Declare @itemsCount int
Select @itemsCount = count(*) From product where id in (1,2,3)
Declare @totalShippingPrice Decimal(18,2) = 9.95
Declare @shippingPriceperItem Decimal(18,2) = @totalShippingPrice / @itemsCount

Update Product 
set [shippingPrice] = @shippingPriceperItem
Where id in (1,2,3)
select * from product
于 2012-11-08T15:38:38.580 に答える