Untitled

mail@pastecode.io avatar
unknown
sql
9 days ago
2.2 kB
1
Indexable
Never
CREATE FUNCTION [dbo].[GetEntityDataByDateRange]
(
    @StartDate DATE,
    @EndDate DATE
)
RETURNS TABLE
AS
RETURN
(
    SELECT 
        date,
        entity_id,
        source_id,
        GeneratorPF,
        GeneratorEarthCurrent,
        EngineSpeed,
        ChargeAlternatorVolt,
        EngineBetteryVolt,
        CoolantTemp,
        EngineRunHr3,
        GeneratorL1Current,
        GeneratorL2_L3Volt,
        GeneratorL3_L1Volt,
        GeneratorKVAHour,
        OilPressure,
        GeneratorL1_NVolt,
        GeneratorL3Current,
        GeneratorL3_NVolt,
        dbo.udsvf_DG_Run_Hr([entity_id], date) AS [RunningHour]
    FROM (
        SELECT 
            CONVERT(date, time) AS date,
            * 
        FROM
            [ITC_PCPB_ULB_UTILITY_TW].[dbo].[stream] a 
        WHERE 
            CONVERT(date, a.time) BETWEEN @StartDate AND @EndDate
            AND a.entity_id LIKE '%DG%'
    ) AS a
    CROSS APPLY OPENJSON(a.field_values, '$')
    WITH (
        GeneratorPF float '$.GeneratorPF',
        GeneratorEarthCurrent float '$.GeneratorEarthCurrent',
        GeneratorKVArHour float '$.GeneratorKVArHour',
        GeneratorL2_NVolt float '$.GeneratorL2_NVolt',
        EngineSpeed float '$.EngineSpeed',
        ChargeAlternatorVolt float '$.ChargeAlternatorVolt',
        EngineBetteryVolt float '$.EngineBetteryVolt',
        CoolantTemp float '$.CoolantTemp',
        EngineRunHr3 float '$.EngineRunHr3',
        GeneratorL1Current float '$.GeneratorL1Current',
        GeneratorL2_L3Volt float '$.GeneratorL2_L3Volt',
        GeneratorL3_L1Volt float '$.GeneratorL3_L1Volt',
        GeneratorKVAHour float '$.GeneratorKVAHour',
        GeneratorL2Current float '$.GeneratorL2Current',
        OilPressure float '$.OilPressure',
        GeneratorL1_NVolt float '$.GeneratorL1_NVolt',
        GeneratorL3Current float '$.GeneratorL3Current',
        GeneratorL3_NVolt float '$.GeneratorL3_NVolt',
        GeneratorL1_L2Volt float '$.GeneratorL1_L2Volt',
        GeneratorKWHour float '$.GeneratorKWHour',
        GenFrequency float '$.GenFrequency',
        Fuellevel float '$.Fuellevel'
    ) AS t
    ORDER BY entity_id
);
Leave a Comment