存储过程中计算的SQL列

时间:2015-02-27 04:18:33

标签: sql sql-server-2008 stored-procedures sql-server-2008-r2

我有以下存储过程:

SELECT 
    a.OtherMeasurementID
    , a.MeasurementID
    , b.MeasurementCode
    , a.OperationDate
    , a.OperationTime
    , a.AccumulatedMass 
    , CAST(a.OperationDate AS datetime) + CAST(a.OperationTime as datetime) oDateTime
INTO 
    #_OtherMeasurement
FROM 
    _OtherMeasurement a 
INNER JOIN 
    _Measurement b ON a.MeasurementID = b.MeasurementID
WHERE 
    a.OperationDate >= DATEADD(DAY, -1, @StartDate) 
    AND a.OperationDate <= DATEADD(DAY, 1, @EndDate)
    AND b.MeasurementCode LIKE '%Inlet%'

--============================================
;with CTE as
(
    select 
        T1.OtherMeasurementID
        , t1.MeasurementID
        , T1.MeasurementCode
        , T1.OperationDate
        , T1.OperationTime
        , T1.AccumulatedMass
        , case when T1.AccumulatedMass = 0 then 0 else T1.AccumulatedMass - T2.AccumulatedMass end as ReceivedMass
        , T1.oDateTime
    From 
        #_OtherMeasurement T1
    left join
        #_OtherMeasurement T2 on T1.MeasurementCode = T2.MeasurementCode 
                    and DATEADD(Hour, -1, T1.oDateTime) = T2.oDateTime
    left join 
        #_OtherMeasurement T3 on T1.MeasurementCode = T3.MeasurementCode 
                    and DATEADD(Hour, 1, T1.oDateTime)= T3.oDateTime
)
,CTE2 as
(      
    select 
        T1.OtherMeasurementID
        , T1.MeasurementID
        , T1.MeasurementCode
        , T1.OperationDate
        , T1.OperationTime
        , T1.AccumulatedMass             
        , case when T1.AccumulatedMass = 0 then (T2.ReceivedMass + T3.ReceivedMass)/2 else T1.ReceivedMass end as ReceivedMass        
    From 
        CTE T1
    left join 
        CTE T2 on T1.OperationDate = T2.OperationDate and T1.MeasurementCode = T2.MeasurementCode 
                and DATEADD(Hour, -1, T1.oDateTime)= T2.oDateTime
    left join 
        CTE T3 on T1.OperationDate = T3.OperationDate and T1.MeasurementCode = T3.MeasurementCode 
                and DATEADD(Hour, 1, T1.oDateTime)= T3.oDateTime 
)
select 
    OtherMeasurementID,
    MeasurementID,
    MeasurementCode,
    OperationDate,
    OperationTime,
    AccumulatedMass,
    coalesce(ReceivedMass, 0) ReceivedMass
from 
    CTE2
WHERE
    OperationDate BETWEEN @StartDate AND @EndDate

如果你在CTE2上看到,那就是

case when T1.AccumulatedMass = 0 then (T2.ReceivedMass + T3.ReceivedMass)/2 else T1.ReceivedMass end as ReceivedMass

如果在同一天的累计质量值为零,那么该行将起作用。该表的数据是每小时基数。如果零值是在2015年1月10日23:00:00,那么该线路将无法正常工作。因为下一个值应该是2015年1月11日00:00:00。示例声明是,如果零之前和之后的值在同一天,则计算将起作用。如果没有,那就不行了。

我的问题是,如果零在不同的日期之后我怎么能使公式工作甚至是下一个值..?

实施例:

OtherMeasurementID  MeasurementID   MeasurementCode OperationDate   OperationTime   AccumulatedMass ReceivedMass
17                       1         Water Inlet         13-Oct-2014  17:00:00           236265.36    518.58
18                       1         Water Inlet         13-Oct-2014  18:00:00           236795.95    530.59
19                       1         Water Inlet         13-Oct-2014  19:00:00           0            533.57
20                       1         Water Inlet         13-Oct-2014  20:00:00           237865.13    536.55
21                       1         Water Inlet         13-Oct-2014  21:00:00           238404.14    539.01
22                       1         Water Inlet         13-Oct-2014  22:00:00           238944.05    539.91
23                       1         Water Inlet         13-Oct-2014  23:00:00           0            0
24                       1         Water Inlet         14-Oct-2014  0:00:00            240026       240026
25                       1         Water Inlet         14-Oct-2014  1:00:00            240566.98    540.98
26                       1         Water Inlet         14-Oct-2014  2:00:00            241107.92    540.94

如果你能看到上面的例子。在2015年10月13日19:00,累积质量为零。所以计算接收质量的公式是(previous receivedmass before zero + next receivedmass after zero) / 2

但是,如果您在2015年10月13日23:00看到,上面的公式不起作用,因为next receivedmass after zero是另一个日期,即2015年10月14日00:00

谢谢。

1 个答案:

答案 0 :(得分:0)

简单的改变:

left join CTE T3 on T1.OperationDate = T3.OperationDate and T1.MeasurementCode = T3.MeasurementCode and DATEADD(Hour, 1, T1.oDateTime)= T3.oDateTime

于:

left join CTE T3 on (T1.OperationDate = T3.OperationDate and T1.MeasurementCode = T3.MeasurementCode and DATEADD(Hour, 1, T1.oDateTime)= T3.oDateTime) or (dateadd(day, 1, T1.OperationDate) = T3.OperationDate and T1.MeasurementCode = T3.MeasurementCode and DATEADD(Hour, 1, T1.oDateTime)= T3.oDateTime)