WITH QuantityMap AS
(
    SELECT 'I1 Waveform' AS Quantity, 'Current' AS MeasurementType, 'Instantaneous' AS MeasurementCharacteristic, 'AN' AS Phase, 'Values' AS SeriesType UNION ALL
    SELECT 'I2 Waveform' AS Quantity, 'Current' AS MeasurementType, 'Instantaneous' AS MeasurementCharacteristic, 'BN' AS Phase, 'Values' AS SeriesType UNION ALL
    SELECT 'I3 Waveform' AS Quantity, 'Current' AS MeasurementType, 'Instantaneous' AS MeasurementCharacteristic, 'CN' AS Phase, 'Values' AS SeriesType UNION ALL
    SELECT 'V1 Waveform' AS Quantity, 'Voltage' AS MeasurementType, 'Instantaneous' AS MeasurementCharacteristic, 'AN' AS Phase, 'Values' AS SeriesType UNION ALL
    SELECT 'V2 Waveform' AS Quantity, 'Voltage' AS MeasurementType, 'Instantaneous' AS MeasurementCharacteristic, 'BN' AS Phase, 'Values' AS SeriesType UNION ALL
    SELECT 'V3 Waveform' AS Quantity, 'Voltage' AS MeasurementType, 'Instantaneous' AS MeasurementCharacteristic, 'CN' AS Phase, 'Values' AS SeriesType
)
SELECT
    Source.ID AS SourceID,
    Source.Name AS Source,
    Quantity.Name AS Quantity,
    QuantityMap.MeasurementType,
    QuantityMap.MeasurementCharacteristic,
    QuantityMap.Phase,
    QuantityMap.SeriesType,
    WaveformLog2.TimeOfFirstPointUTC,
    WaveformLog2.TimeOfLastPointUTC,
    WaveformLog2.SamplingFrequency,
    WaveformLog2.Scale,
    WaveformLog2.Offset,
    WaveformLog2.DataPointType,
    WaveformLog2.Samples
FROM
    WaveformLog2 JOIN
    Quantity ON WaveformLog2.QuantityID = Quantity.ID JOIN
    QuantityMap ON Quantity.Name = QuantityMap.Quantity JOIN
    Source ON WaveformLog2.SourceID = Source.ID JOIN
    SourceType ON Source.SourceTypeID = SourceType.ID
WHERE SourceType.Name = '8650'