乘以第+ + 2列的值

时间:2016-12-07 02:32:36

标签: sql sql-server date sql-server-2014

我有一张这样的表:

if(CollectionUtils.isEmpty(...)) {...}

我想填写 cpo_sell_profit 列,但我不知道如何将 production_ms 值与 cpo 中的值相乘,其中cpo。 date = day + 2以填充 cpo_sell_profit

示例:

从2016-08-01算起,75187从2016-08-03乘以7330

提前致谢

5 个答案:

答案 0 :(得分:1)

你可以试试这个

UPDATE t2
SET t2.cpo_sell_profit = t1.production_ms *t2.cpo
From yourtable t1 inner join yourtable t2 on t1.date = dateadd(dd,2,t2.date)

答案 1 :(得分:1)

;with cte as (
    Select *
          ,NewVal = production_ms* Lead(cpo,2,0) over (Order By date)
     From  YourTable
)
Update cte Set cpo_sell_profit = NewVal

Select * from YourTable

返回

date        cpo     production_ms   cpo_sell_profit
2016-08-01  7146    75187           551120710
2016-08-02  7299    68925           511147800
2016-08-03  7330    65534           495633642
2016-08-04  7416    72133           0
2016-08-05  7563    71442           0

不确定你想要对超出范围的记录做什么。目前设置为零,但是如果你输入1,你将得到production_ms ..请参阅Lead(cpo,2, 0)

答案 2 :(得分:1)

使用LEAD功能

;WITH cte
     AS (SELECT *,
                Lead(cpo, 2)OVER(ORDER BY dates) AS next_val
         FROM   (VALUES ('2016-08-01',7146,75187 ),
                        ('2016-08-02',7299,68925 ),
                        ('2016-08-03',7330,65534 ),
                        ('2016-08-04',7416,72133 ),
                        ('2016-08-05',7563,71442 )) tc(dates, cpo, production_ms ))
SELECT dates,
       cpo,
       production_ms,
       production_ms * next_val
FROM   cte 

结果:

╔════════════╦══════╦═══════════════╦═════════════════╗
║   dates    ║ cpo  ║ production_ms ║ cpo_sell_profit ║
╠════════════╬══════╬═══════════════╬═════════════════╣
║ 2016-08-01 ║ 7146 ║         75187 ║ 551120710       ║
║ 2016-08-02 ║ 7299 ║         68925 ║ 511147800       ║
║ 2016-08-03 ║ 7330 ║         65534 ║ 495633642       ║
║ 2016-08-04 ║ 7416 ║         72133 ║ NULL            ║
║ 2016-08-05 ║ 7563 ║         71442 ║ NULL            ║
╚════════════╩══════╩═══════════════╩═════════════════╝

如果在production_ms没有2 +日期时您想要相同的值,请使用ISNULL

SELECT dates,
       cpo,
       production_ms,
       production_ms * ISNULL(next_val,1)
FROM   cte 

结果:

╔════════════╦══════╦═══════════════╦═════════════════╗
║   dates    ║ cpo  ║ production_ms ║ cpo_sell_profit ║
╠════════════╬══════╬═══════════════╬═════════════════╣
║ 2016-08-01 ║ 7146 ║         75187 ║       551120710 ║
║ 2016-08-02 ║ 7299 ║         68925 ║       511147800 ║
║ 2016-08-03 ║ 7330 ║         65534 ║       495633642 ║
║ 2016-08-04 ║ 7416 ║         72133 ║           72133 ║
║ 2016-08-05 ║ 7563 ║         71442 ║           71442 ║
╚════════════╩══════╩═══════════════╩═════════════════╝

答案 3 :(得分:0)

您可以在这种情况下简单地应用交叉应用,这不需要任何公用表表达式和临时表

UPDATE [cpo] set cpo_sell_profit = csp.cpo_sell_profit FROM [cpo] src
CROSS APPLY 
(
    SELECT (src.production_ms * (SELECT production_ms FROM [cpo] WHERE date = DATEADD(day,2, src.date))) AS cpo_sell_profit
) AS csp

答案 4 :(得分:0)

试试这个,

declare @t table(dates date,cpo int,production_ms int,cpo_sell_profit bigint)
insert into @t values
('2016-08-01',7146,75187 ,null) 
,('2016-08-02',7299,68925,null) 
,('2016-08-03',7330,65534,null) 
,('2016-08-04',7416,72133,null) 
,('2016-08-05',7563,71442,null)
;With CTE as
(
select *,ROW_NUMBER()over(order by dates)rn from @t
)


select c.dates,c.cpo,c.production_ms
,c.production_ms*c1.cpo  cpo_sell_profit from cte c
left join cte c1 on c.rn+2=c1.rn

update t
set cpo_sell_profit=t.production_ms*t1.cpo
from @t t
left join @t t1 on dateadd(day,2,t.dates)=t1.dates
select * from @t