WITH QuantityMap AS
(
    SELECT 'V avg high' AS Quantity, 'Voltage' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'LineToNeutralAverage' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'V avg low' AS Quantity, 'Voltage' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'LineToNeutralAverage' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'V avg mean' AS Quantity, 'Voltage' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'LineToNeutralAverage' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'V1 high' AS Quantity, 'Voltage' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'AN' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'V1 low' AS Quantity, 'Voltage' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'AN' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'V1 mean' AS Quantity, 'Voltage' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'AN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'V2 high' AS Quantity, 'Voltage' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'BN' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'V2 low' AS Quantity, 'Voltage' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'BN' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'V2 mean' AS Quantity, 'Voltage' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'BN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'V3 high' AS Quantity, 'Voltage' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'CN' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'V3 low' AS Quantity, 'Voltage' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'CN' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'V3 mean' AS Quantity, 'Voltage' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'CN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Current N High' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'NG' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Current N Low' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'NG' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'Current N Mean' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'NG' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Current Phase A High' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'AN' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Current Phase A Low' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'AN' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'Current Phase A Mean' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'AN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Current Phase Average High' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'LineToNeutralAverage' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Current Phase Average Low' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'LineToNeutralAverage' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'Current Phase Average Mean' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'LineToNeutralAverage' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Current Phase B High' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'BN' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Current Phase B Low' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'BN' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'Current Phase B Mean' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'BN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Current Phase C High' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'CN' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Current Phase C Low' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'CN' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'Current Phase C Mean' AS Quantity, 'Current' AS MeasurementType, 'RMS' AS MeasurementCharacteristic, 'CN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Active Power High' AS Quantity, 'Power' AS MeasurementType, 'P' AS MeasurementCharacteristic, 'Total' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Active Power Low' AS Quantity, 'Power' AS MeasurementType, 'P' AS MeasurementCharacteristic, 'Total' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'Active Power Mean' AS Quantity, 'Power' AS MeasurementType, 'P' AS MeasurementCharacteristic, 'Total' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Apparent Power High' AS Quantity, 'Power' AS MeasurementType, 'S' AS MeasurementCharacteristic, 'Total' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Apparent Power Low' AS Quantity, 'Power' AS MeasurementType, 'S' AS MeasurementCharacteristic, 'Total' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'Apparent Power Mean' AS Quantity, 'Power' AS MeasurementType, 'S' AS MeasurementCharacteristic, 'Total' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Reactive Power High' AS Quantity, 'Power' AS MeasurementType, 'Q' AS MeasurementCharacteristic, 'Total' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Reactive Power Low' AS Quantity, 'Power' AS MeasurementType, 'Q' AS MeasurementCharacteristic, 'Total' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'Reactive Power Mean' AS Quantity, 'Power' AS MeasurementType, 'Q' AS MeasurementCharacteristic, 'Total' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'V1 THD low' AS Quantity, 'Voltage' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'AN' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'V2 THD low' AS Quantity, 'Voltage' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'BN' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'V3 THD low' AS Quantity, 'Voltage' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'CN' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'Voltage Total Harmonic Distortion High on Input V1' AS Quantity, 'Voltage' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'AN' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Voltage Total Harmonic Distortion High on Input V2' AS Quantity, 'Voltage' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'BN' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Voltage Total Harmonic Distortion High on Input V3' AS Quantity, 'Voltage' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'CN' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Voltage Total Harmonic Distortion 10-minute Mean on Input V1' AS Quantity, 'Voltage' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'AN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Voltage Total Harmonic Distortion 10-minute Mean on Input V2' AS Quantity, 'Voltage' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'BN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Voltage Total Harmonic Distortion 10-minute Mean on Input V3' AS Quantity, 'Voltage' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'CN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'I1 THD 10m' AS Quantity, 'Current' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'AN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'I2 THD 10m' AS Quantity, 'Current' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'BN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'I3 THD 10m' AS Quantity, 'Current' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'CN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'I1 THD low' AS Quantity, 'Current' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'AN' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'I2 THD low' AS Quantity, 'Current' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'BN' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'I3 THD low' AS Quantity, 'Current' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'CN' AS Phase, 'Minimum' AS SeriesType UNION ALL
    SELECT 'Current Total Harmonic Distortion Phase A High' AS Quantity, 'Current' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'AN' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Current Total Harmonic Distortion Phase B High' AS Quantity, 'Current' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'BN' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Current Total Harmonic Distortion Phase C High' AS Quantity, 'Current' AS MeasurementType, 'TotalTHD' AS MeasurementCharacteristic, 'CN' AS Phase, 'Maximum' AS SeriesType UNION ALL
    SELECT 'Voltage Flicker Plt on Input V1' AS Quantity, 'Voltage' AS MeasurementType, 'FlkrPLT' AS MeasurementCharacteristic, 'AN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Voltage Flicker Plt on Input V2' AS Quantity, 'Voltage' AS MeasurementType, 'FlkrPLT' AS MeasurementCharacteristic, 'BN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Voltage Flicker Plt on Input V3' AS Quantity, 'Voltage' AS MeasurementType, 'FlkrPLT' AS MeasurementCharacteristic, 'CN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Voltage Flicker Pst on Input V1' AS Quantity, 'Voltage' AS MeasurementType, 'FlkrPST' AS MeasurementCharacteristic, 'AN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Voltage Flicker Pst on Input V2' AS Quantity, 'Voltage' AS MeasurementType, 'FlkrPST' AS MeasurementCharacteristic, 'BN' AS Phase, 'Average' AS SeriesType UNION ALL
    SELECT 'Voltage Flicker Pst on Input V3' AS Quantity, 'Voltage' AS MeasurementType, 'FlkrPST' AS MeasurementCharacteristic, 'CN' AS Phase, 'Average' AS SeriesType
)
SELECT
    Source.ID AS SourceID,
    Source.Name AS Source,
    Quantity.Name AS Quantity,
    QuantityMap.MeasurementType,
    QuantityMap.MeasurementCharacteristic,
    QuantityMap.Phase,
    QuantityMap.SeriesType,
    DataLog2.TimestampUTC,
    DataLog2.Value
FROM
    DataLog2 JOIN
    Quantity ON DataLog2.QuantityID = Quantity.ID JOIN
    QuantityMap ON Quantity.Name = QuantityMap.Quantity JOIN
    Source ON DataLog2.SourceID = Source.ID JOIN
    SourceType ON Source.SourceTypeID = SourceType.ID
WHERE SourceType.Name = '8650'