簡體   English   中英

如何使用多個case語句更新多個值null

[英]how to update multiple values null using multiple case statements

這是我的表:

Site_name | date& Time              | PowerOutput         
----------+-------------------------+------------------
ACT0001   | 2013-07-21 01:00:00.000 | 196.852984494331   
ACT0001   | 2013-07-21 02:00:00.000 |   0   
xyz0001   | 2013-07-21 03:00:00.000 | 196.852984494331   
xyq0001   | 2013-07-21 04:00:00.000 | 196.958395639561 
xys0001   | 2013-07-21 05:00:00.000 |   0
xyd0001   | 2013-07-21 06:00:00.000 | 197.20098185022 
xye0001   | 2013-07-21 07:00:00.000 |   0 
xyg0001   | 2013-07-21 08:00:00.000 |   0     
cfg0001   | 2013-07-21 09:00:00.000 | 197.412144323522 
acb0001   | 2013-07-21 10:00:00.000 |   0 
bdf0001   | 2013-07-21 11:00:00.000 |   0 
olk0001   | 2013-07-21 12:00:00.000 | 196.886233049016 

我有這個表,我正在嘗試更新“ZERO”的地方的值。 如果只有一個零,那么我可以更新表,但如果有連續的零,我發現很難更新表。

邏輯是:

  • ((先前值 - 下一個值)/前一個值)* 100 <5

    如果這是真的,那么它應該插入前一個值

  • ((上一個值 - 下一個值)/前一個值)* 100> = 5

    如果這是真的那么它應該保持為零。

這是我到目前為止的代碼:

with cte as
(
    SELECT  
        *,
        lead(pr_output,1) OVER (ORDER BY (select null)) As PreviousValue,
        lag(pr_output,1) OVER (ORDER BY (select null)) As NextValue
    FROM
        [dbo].[Mytable]
)
,ctee as
(
    select 
        *,
        abs((PreviousValue*100-NextValue*100)/NextValue) as CheckFlag
    from
        cte 
)
select
    Site_name,[DATE&Time],
    case 
        when pr_output <>0 then pr_output
        else
            case 
                when CheckFlag >= 5 then 0
                else PreviousValue
            end
    end as pr_output
from 
    ctee

我得到的錯誤是

遇到零除錯誤。

請告訴我我做錯了什么。 如果他們是通過這種方式的替代方式?

任何幫助表示贊賞。

輸出應該是:

Site_name | date& Time              | PowerOutput         
----------+-------------------------+------------------
ACT0001   | 2013-07-21 01:00:00.000 | 196.852984494331   
ACT0001   | 2013-07-21 02:00:00.000 | 196.852984494331   
xyz0001   | 2013-07-21 03:00:00.000 | 196.852984494331   
xyq0001   | 2013-07-21 04:00:00.000 | 196.958395639561 
xys0001   | 2013-07-21 05:00:00.000 | 196.958395639561
xyd0001   | 2013-07-21 06:00:00.000 | 197.20098185022 
xye0001   | 2013-07-21 07:00:00.000 |   0 
xyg0001   | 2013-07-21 08:00:00.000 |   0     
cfg0001   | 2013-07-21 09:00:00.000 | 197.412144323522 
acb0001   | 2013-07-21 10:00:00.000 |   0
bdf0001   | 2013-07-21 11:00:00.000 |   0 
olk0001   | 2013-07-21 12:00:00.000 | 196.886233049016 

在除以之前需要檢查NextValue是否為零..

希望這能解決你的問題。

with cte as
(
    SELECT  
        *,
        lead(pr_output,1) OVER (ORDER BY (select null)) As PreviousValue,
        lag(pr_output,1) OVER (ORDER BY (select null)) As NextValue
    FROM
        [dbo].[Mytable]
)
,ctee as
(
    select 
        *,
        abs((PreviousValue*100-NextValue*100)/(case when NextValue = 0 then 1 else NextValue end)) as CheckFlag
    from
        cte 
)
select
    Site_name,[DATE&Time],
    case 
        when pr_output <>0 then pr_output
        else
            case 
                when CheckFlag >= 5 then 0
                else PreviousValue
            end
    end as pr_output
from 
    ctee

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM