2016-12-07 27 views
0

我有這樣的一個表:乘值,其中天+ 2

date  | cpo  | production_ms | cpo_sell_profit 
======================================================= 
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   | 

我想填補cpo_sell_profit列,但我不知道怎麼乘production_ms值與值從cpo其中cpo.date = day + 2爲了填補cpo_sell_profit

例如:

75187從7330 2016年8月1日乘從2016年8月3日

在此先感謝

+0

如果您使用您正在使用的SQL Server版本標記問題,這將有所幫助。 LAG/LEAD可能在這裏派上用場,但它們僅在2012年推出。 –

+0

您正在使用的是哪個版本的sql server –

+0

目前我正在使用sql server 2014 sir – Jsnow

回答

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 ..看到鉛(CPO,2,0)

+0

當我們在每個月的最後一天時,我們是否可以獲得下個月的價值? – Jsnow

+0

@Jsnow是的,這將是一個投擲兩個日子裏,無論月底的 –

+0

@Jsnow戈登好點。這些日子是連續的,沒有重複的日期? –

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) 
+0

'join'比主角更安全,除非OP能保證日期中沒有空白。 –

+0

我不明白差距意思是先生 – Jsnow

+2

OMG,@GordonLinoff評論我的答案。希望太高了:) – DarkKnight

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 ║ 
╚════════════╩══════╩═══════════════╩═════════════════╝ 
+0

請問先生是什麼;與CTE裝置noob問題 – Jsnow

+0

對不起@Jsnow'CTE'是一個臨時的結果集,它可以是自引用,並且可以在同一個查詢中多次引用。檢查這裏獲取更多信息https://technet.microsoft.com/en-us/library/ms190766(v=sql.105).aspx –

+0

在八月thare是31天,我們可以從2016-09-的cpo_sell_profit值02先生?或者它在月底總是空? – Jsnow

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 
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