-- ------------------------------------------------------------------------------------
-- ION Data v5.0 Database Upgrade: Step 10
--
-- Updates all Power Quality stored procedures affected by the database schema updates.
-- ------------------------------------------------------------------------------------
	SELECT dbo.FormatStatusMessage('Step 10: Started.')
GO
ALTER PROCEDURE dbo.spDAL_GetEN50160MainsSignalingData
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID INT PRIMARY KEY NOT NULL,
		Name NVARCHAR(250) NOT NULL,
		Namespace NVARCHAR(400) NOT NULL,
		Description NVARCHAR(2000),
		TimeZoneID SMALLINT NOT NULL
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		Name,
		Namespace,
		Description,
		TimeZoneID
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)
	 
	-- put the data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME,
		Value			FLOAT
	)

	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		q.Name IN
		(
			N'Voltage Mains Signaling Count N on Input V1',
			N'Voltage Mains Signaling Count N1 on Input V1',
			N'Voltage Mains Signaling Count N invalid on Input V1',
			N'Voltage Mains Signaling Count N on Input V2',
			N'Voltage Mains Signaling Count N1 on Input V2',
			N'Voltage Mains Signaling Count N invalid on Input V2',
			N'Voltage Mains Signaling Count N on Input V3',
			N'Voltage Mains Signaling Count N1 on Input V3',
			N'Voltage Mains Signaling Count N invalid on Input V3'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_MainsSignaling TABLE
	(
		TimestampUtc				DATETIME,
		SourceID					INT,
		V1MainsSignalingN			FLOAT,
		V1MainsSignalingN1			FLOAT,
		V1MainsSignalingNInvalid	FLOAT,
		V1MainsSignalingN1Eval		FLOAT,
		V2MainsSignalingN			FLOAT,
		V2MainsSignalingN1			FLOAT,
		V2MainsSignalingNInvalid	FLOAT,
		V2MainsSignalingN1Eval		FLOAT,
		V3MainsSignalingN			FLOAT,
		V3MainsSignalingN1			FLOAT,
		V3MainsSignalingNInvalid	FLOAT,
		V3MainsSignalingN1Eval		FLOAT
	)

	INSERT INTO
		@EN50160_MainsSignaling
	SELECT
		TimestampUtc,
		SourceID,
		[Voltage Mains Signaling Count N on Input V1]			AS V1MainsSignalingN,
		[Voltage Mains Signaling Count N1 on Input V1]			AS V1MainsSignalingN1,
		[Voltage Mains Signaling Count N invalid on Input V1]	AS V1MainsSignalingNInvalid,
		CASE [Voltage Mains Signaling Count N on Input V1]
			WHEN 0 THEN NULL
			ELSE [Voltage Mains Signaling Count N1 on Input V1]/[Voltage Mains Signaling Count N on Input V1]
		END AS V1MainsSignalingN1Eval,
		[Voltage Mains Signaling Count N on Input V2]			AS V2MainsSignalingN,
		[Voltage Mains Signaling Count N1 on Input V2]			AS V2MainsSignalingN1,
		[Voltage Mains Signaling Count N invalid on Input V2]	AS V2MainsSignalingNInvalid,
		CASE [Voltage Mains Signaling Count N on Input V2]
			WHEN 0 THEN NULL
			ELSE [Voltage Mains Signaling Count N1 on Input V2]/[Voltage Mains Signaling Count N on Input V2]
		END AS V2MainsSignalingN1Eval,
		[Voltage Mains Signaling Count N on Input V3]			AS V3MainsSignalingN,
		[Voltage Mains Signaling Count N1 on Input V3]			AS V3MainsSignalingN1,
		[Voltage Mains Signaling Count N invalid on Input V3]	AS V3MainsSignalingNInvalid,
		CASE [Voltage Mains Signaling Count N on Input V3]
			WHEN 0 THEN NULL
			ELSE [Voltage Mains Signaling Count N1 on Input V3]/[Voltage Mains Signaling Count N on Input V3]
		END AS V3MainsSignalingN1Eval
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[Voltage Mains Signaling Count N on Input V1],
			[Voltage Mains Signaling Count N1 on Input V1],
			[Voltage Mains Signaling Count N invalid on Input V1],
			[Voltage Mains Signaling Count N on Input V2],
			[Voltage Mains Signaling Count N1 on Input V2],
			[Voltage Mains Signaling Count N invalid on Input V2],
			[Voltage Mains Signaling Count N on Input V3],
			[Voltage Mains Signaling Count N1 on Input V3],
			[Voltage Mains Signaling Count N invalid on Input V3]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID

	-- return the EN50160 data for the sources we have selected
	SELECT
		TimestampUtc,
		SourceID,
		V1MainsSignalingN,
		V1MainsSignalingN1,
		V1MainsSignalingNInvalid,
		V1MainsSignalingN1Eval,
		V2MainsSignalingN,
		V2MainsSignalingN1,
		V2MainsSignalingNInvalid,
		V2MainsSignalingN1Eval,
		V3MainsSignalingN,
		V3MainsSignalingN1,
		V3MainsSignalingNInvalid,
		V3MainsSignalingN1Eval
	FROM
		@EN50160_MainsSignaling

GO

/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Flicker data for the devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160FlickerDataPL.sql#2 $
******************************************************************************/
ALTER PROCEDURE [dbo].[spDAL_GetEN50160FlickerDataPL]
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

-- find information about the sources we have selected
DECLARE @TblSources TABLE
(
	ID							INT PRIMARY KEY NOT NULL,
	MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
)

INSERT INTO
	@TblSources
SELECT
	ID,
	UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
FROM
	dbo.udfDAL_GetSources(@SourceIDs)

-- define the character that we use to separate the different dates
-- within the lists of start and end dates
DECLARE @DelimiterCharacter NCHAR(1)
SET @DelimiterCharacter = CHAR(6)

-- create a table variable to hold the EN50160 results
DECLARE @EN50160_Flicker TABLE
(
	TimestampUtc		DATETIME,
	SourceID			INT,
    V1FlickerN1Eval		FLOAT,
    V2FlickerN1Eval		FLOAT,
    V3FlickerN1Eval		FLOAT    
)
 
-- put the raw data in a form that's easier to query
DECLARE @TblData TABLE
(
	SourceID		INT,
	MeasurementName	NVARCHAR(200),
	TimestampUtc	DATETIME2,
	Value			FLOAT
)

INSERT INTO
	@TblData
SELECT
	s.ID AS SourceID,
	q.Name AS MeasurementName,
	dl.TimestampUTC AS TimestampUtc,
	dl.Value
FROM
	dbo.DataLog2 dl
	INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
	INNER JOIN @TblSources s on dl.SourceID = s.ID
WHERE
	s.MeasurementSetIdentifier = N'PL'
	AND q.Name IN
	(
		N'V1 Flicker N1 Eval',
		N'V2 Flicker N1 Eval',
		N'V3 Flicker N1 Eval'
	)
	AND EXISTS
	(
		SELECT
			1
		FROM
			dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
		WHERE
			dl.TimestampUTC > ts.StartDate 
			AND dl.TimestampUTC <= ts.EndDate
	)
ORDER BY
	TimestampUTC,
	SourceID,
	q.Name

-- select data for the PowerLogic (PL) devices into the results table
SELECT
	TimestampUtc,
	SourceID,
	[V1 Flicker N1 Eval]	AS V1FlickerN1Eval,
	[V2 Flicker N1 Eval]	AS V2FlickerN1Eval,
	[V3 Flicker N1 Eval]	AS V3FlickerN1Eval
FROM
	@TblData
PIVOT
(
	MAX(Value)
	FOR MeasurementName IN
	(
		[V1 Flicker N1 Eval],
		[V2 Flicker N1 Eval],
		[V3 Flicker N1 Eval]
	)
) AS pvt
ORDER BY
	TimestampUtc,
	SourceID

GO

/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Harmonic Voltage data for the devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160HarmonicVoltageDataION.sql#2 $
******************************************************************************/
ALTER PROCEDURE [dbo].[spDAL_GetEN50160HarmonicVoltageDataION]
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID							INT PRIMARY KEY NOT NULL,
		MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_HarmonicVoltage TABLE
	(
		TimestampUtc				DATETIME2,
		SourceID					INT,
        V1HarmonicVoltageN			FLOAT,
        V1HarmonicVoltageN1			FLOAT,
        V1HarmonicVoltageN2			FLOAT,
        V1HarmonicVoltageN1Eval		FLOAT,
        V1HarmonicVoltageN2Eval		FLOAT,
		V1HarmonicVoltageNInvalid	FLOAT,
        V2HarmonicVoltageN			FLOAT,
        V2HarmonicVoltageN1			FLOAT,
        V2HarmonicVoltageN2			FLOAT,
        V2HarmonicVoltageN1Eval		FLOAT,
        V2HarmonicVoltageN2Eval		FLOAT,
		V2HarmonicVoltageNInvalid	FLOAT,
        V3HarmonicVoltageN			FLOAT,
        V3HarmonicVoltageN1			FLOAT,
        V3HarmonicVoltageN2			FLOAT,
        V3HarmonicVoltageN1Eval		FLOAT,
        V3HarmonicVoltageN2Eval		FLOAT,
		V3HarmonicVoltageNInvalid	FLOAT
	)
	 
	-- put the raw data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME2,
		Value			FLOAT
	)
	-- insert data for the ION devices into the staging table
	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		s.MeasurementSetIdentifier = N'ION'
		AND q.Name IN
		(
			N'Voltage Harmonics Count N on Input V1',
			N'Voltage Harmonics Count N1 on Input V1',
			N'Voltage Harmonics Count N2 on Input V1',
			N'Voltage Harmonics Count N invalid on Input V1',
			N'Voltage Harmonics Count N on Input V2',
			N'Voltage Harmonics Count N1 on Input V2',
			N'Voltage Harmonics Count N2 on Input V2',
			N'Voltage Harmonics Count N invalid on Input V2',
			N'Voltage Harmonics Count N on Input V3',
			N'Voltage Harmonics Count N1 on Input V3',
			N'Voltage Harmonics Count N2 on Input V3',
			N'Voltage Harmonics Count N invalid on Input V3'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name

	-- select data for the ION devices into the results table
	SELECT
		TimestampUtc,
		SourceID,
		[Voltage Harmonics Count N on Input V1]			AS V1HarmonicVoltageN,
		[Voltage Harmonics Count N1 on Input V1]		AS V1HarmonicVoltageN1,
		[Voltage Harmonics Count N2 on Input V1]		AS V1HarmonicVoltageN2,
		CASE [Voltage Harmonics Count N on Input V1]
			WHEN 0 THEN NULL
			ELSE [Voltage Harmonics Count N1 on Input V1]/[Voltage Harmonics Count N on Input V1]
		END AS V1HarmonicVoltageN1Eval,
		CASE [Voltage Harmonics Count N on Input V1]
			WHEN 0 THEN NULL
			ELSE [Voltage Harmonics Count N2 on Input V1]/[Voltage Harmonics Count N on Input V1]
		END AS V1HarmonicVoltageN2Eval,
		[Voltage Harmonics Count N invalid on Input V1]	AS V1HarmonicVoltageNInvalid,
		[Voltage Harmonics Count N on Input V2]			AS V2HarmonicVoltageN,
		[Voltage Harmonics Count N1 on Input V2]		AS V2HarmonicVoltageN1,
		[Voltage Harmonics Count N2 on Input V2]		AS V2HarmonicVoltageN2,
		CASE [Voltage Harmonics Count N on Input V2]
			WHEN 0 THEN NULL
			ELSE [Voltage Harmonics Count N1 on Input V2]/[Voltage Harmonics Count N on Input V2]
		END AS V2HarmonicVoltageN1Eval,
		CASE [Voltage Harmonics Count N on Input V2]
			WHEN 0 THEN NULL
			ELSE [Voltage Harmonics Count N2 on Input V2]/[Voltage Harmonics Count N on Input V2]
		END AS V2HarmonicVoltageN2Eval,
		[Voltage Harmonics Count N invalid on Input V2]	AS V2HarmonicVoltageNInvalid,
		[Voltage Harmonics Count N on Input V3]			AS V3HarmonicVoltageN,
		[Voltage Harmonics Count N1 on Input V3]		AS V3HarmonicVoltageN1,
		[Voltage Harmonics Count N2 on Input V3]		AS V3HarmonicVoltageN2,
		CASE [Voltage Harmonics Count N on Input V3]
			WHEN 0 THEN NULL
			ELSE [Voltage Harmonics Count N1 on Input V3]/[Voltage Harmonics Count N on Input V3]
		END AS V3HarmonicVoltageN1Eval,
		CASE [Voltage Harmonics Count N on Input V3]
			WHEN 0 THEN NULL
			ELSE [Voltage Harmonics Count N2 on Input V3]/[Voltage Harmonics Count N on Input V3]
		END AS V3HarmonicVoltageN2Eval,
		[Voltage Harmonics Count N invalid on Input V3]	AS V3HarmonicVoltageNInvalid
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[Voltage Harmonics Count N on Input V1],
			[Voltage Harmonics Count N1 on Input V1],
			[Voltage Harmonics Count N2 on Input V1],
			[Voltage Harmonics Count N invalid on Input V1],
			[Voltage Harmonics Count N on Input V2],
			[Voltage Harmonics Count N1 on Input V2],
			[Voltage Harmonics Count N2 on Input V2],
			[Voltage Harmonics Count N invalid on Input V2],
			[Voltage Harmonics Count N on Input V3],
			[Voltage Harmonics Count N1 on Input V3],
			[Voltage Harmonics Count N2 on Input V3],
			[Voltage Harmonics Count N invalid on Input V3]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID
GO


/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Harmonic Voltage data for the devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160HarmonicVoltageDataPL.sql#2 $
******************************************************************************/
ALTER PROCEDURE [dbo].[spDAL_GetEN50160HarmonicVoltageDataPL]
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID							INT PRIMARY KEY NOT NULL,
		MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_HarmonicVoltage TABLE
	(
		TimestampUtc				DATETIME2,
		SourceID					INT,
        V1HarmonicVoltageN1Eval		FLOAT,
        V2HarmonicVoltageN1Eval		FLOAT,
        V3HarmonicVoltageN1Eval		FLOAT
    )
	 
	-- put the raw data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME2,
		Value			FLOAT
	)
	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		s.MeasurementSetIdentifier = N'PL'
		AND q.Name IN
		(
			N'V1 THD N1 Eval',
			N'V2 THD N1 Eval',
			N'V3 THD N1 Eval'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name

	-- insert data for the PowerLogic (PL) devices into the results table
	SELECT
		TimestampUtc,
		SourceID,
		[V1 THD N1 Eval]	AS V1HarmonicVoltageN1Eval,
		[V2 THD N1 Eval]	AS V2HarmonicVoltageN1Eval,
		[V3 THD N1 Eval]	AS V3HarmonicVoltageN1Eval
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[V1 THD N1 Eval],
			[V2 THD N1 Eval],
			[V3 THD N1 Eval]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID

GO


/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Interharmonic Voltage data for the devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160InterharmonicVoltageDataION.sql#2 $
******************************************************************************/
ALTER PROCEDURE [dbo].[spDAL_GetEN50160InterharmonicVoltageDataION]
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID							INT PRIMARY KEY NOT NULL,
		MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_InterharmonicVoltage TABLE
	(
		TimestampUtc				DATETIME,
		SourceID					INT,
        V1InterharmonicVoltageN			FLOAT,
        V1InterharmonicVoltageN1		FLOAT,
        V1InterharmonicVoltageN1Eval	FLOAT,
        V1InterharmonicVoltageNInvalid	FLOAT,
        V2InterharmonicVoltageN			FLOAT,
        V2InterharmonicVoltageN1		FLOAT,
        V2InterharmonicVoltageN1Eval	FLOAT,
        V2InterharmonicVoltageNInvalid	FLOAT,
        V3InterharmonicVoltageN			FLOAT,
        V3InterharmonicVoltageN1		FLOAT,
        V3InterharmonicVoltageN1Eval	FLOAT,
        V3InterharmonicVoltageNInvalid	FLOAT
	)
	 
	-- put the raw data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME,
		Value			FLOAT
	)
	-- insert data for the ION devices into the staging table
	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		s.MeasurementSetIdentifier = N'ION'
		AND q.Name IN
		(
			N'Voltage Inter-Harmonics Count N on Input V1',
			N'Voltage Inter-Harmonics Count N1 on Input V1',
			N'Voltage Inter-Harmonics Count N invalid on Input V1',
			N'Voltage Inter-Harmonics Count N on Input V2',
			N'Voltage Inter-Harmonics Count N1 on Input V2',
			N'Voltage Inter-Harmonics Count N invalid on Input V2',
			N'Voltage Inter-Harmonics Count N on Input V3',
			N'Voltage Inter-Harmonics Count N1 on Input V3',
			N'Voltage Inter-Harmonics Count N invalid on Input V3'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name

	-- select data for the ION devices into the results table
	SELECT
		TimestampUtc,
		SourceID,
		[Voltage Inter-Harmonics Count N on Input V1]			AS V1InterharmonicVoltageN,
		[Voltage Inter-Harmonics Count N1 on Input V1]			AS V1InterharmonicVoltageN1,
		CASE [Voltage Inter-Harmonics Count N on Input V1]
			WHEN 0 THEN NULL
			ELSE [Voltage Inter-Harmonics Count N1 on Input V1]/[Voltage Inter-Harmonics Count N on Input V1]
		END AS V1InterharmonicVoltageN1Eval,
		[Voltage Inter-Harmonics Count N invalid on Input V1]	AS V1InterharmonicVoltageNInvalid,
		[Voltage Inter-Harmonics Count N on Input V2]			AS V2InterharmonicVoltageN,
		[Voltage Inter-Harmonics Count N1 on Input V2]			AS V2InterharmonicVoltageN1,
		CASE [Voltage Inter-Harmonics Count N on Input V2]
			WHEN 0 THEN NULL
			ELSE [Voltage Inter-Harmonics Count N1 on Input V2]/[Voltage Inter-Harmonics Count N on Input V2]
		END AS V2InterharmonicVoltageN1Eval,
		[Voltage Inter-Harmonics Count N invalid on Input V2]	AS V2InterharmonicVoltageNInvalid,
		[Voltage Inter-Harmonics Count N on Input V3]			AS V3InterharmonicVoltageN,
		[Voltage Inter-Harmonics Count N1 on Input V3]			AS V3InterharmonicVoltageN1,
		CASE [Voltage Inter-Harmonics Count N on Input V3]
			WHEN 0 THEN NULL
			ELSE [Voltage Inter-Harmonics Count N1 on Input V3]/[Voltage Inter-Harmonics Count N on Input V3]
		END AS V3InterharmonicVoltageN1Eval,
		[Voltage Inter-Harmonics Count N invalid on Input V3]	AS V3InterharmonicVoltageNInvalid
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[Voltage Inter-Harmonics Count N on Input V1],
			[Voltage Inter-Harmonics Count N1 on Input V1],
			[Voltage Inter-Harmonics Count N invalid on Input V1],
			[Voltage Inter-Harmonics Count N on Input V2],
			[Voltage Inter-Harmonics Count N1 on Input V2],
			[Voltage Inter-Harmonics Count N invalid on Input V2],
			[Voltage Inter-Harmonics Count N on Input V3],
			[Voltage Inter-Harmonics Count N1 on Input V3],
			[Voltage Inter-Harmonics Count N invalid on Input V3]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID

	-- NOTE:  PowerLogic (PL) devices do not support Interharmonic Voltage.
GO


/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Power Frequency data for the devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160PowerFrequencyDataION.sql#2 $
******************************************************************************/
ALTER PROCEDURE [dbo].[spDAL_GetEN50160PowerFrequencyDataION]
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID							INT PRIMARY KEY NOT NULL,
		MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_PowerFrequency TABLE
	(
		TimestampUtc			DATETIME,
		SourceID				INT,
        PowerFrequencyN			FLOAT,
        PowerFrequencyN1		FLOAT,
        PowerFrequencyN1Eval	FLOAT,
        PowerFrequencyN2		FLOAT,
        PowerFrequencyN2Eval	FLOAT,
        PowerFrequencyNInvalid	FLOAT
	)
	 
	-- put the raw data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME,
		Value			FLOAT
	)
	-- insert data for the ION devices into the staging table
	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		s.MeasurementSetIdentifier = N'ION'
		AND q.Name IN
		(
			N'Frequency N Count',
			N'Frequency N1 Count',
			N'Frequency N2 Count',
			N'Frequency N invalid Count'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name
	
	-- select data for the ION devices into the results table
	SELECT
		TimestampUtc,
		SourceID,
        [Frequency N Count]			AS PowerFrequencyN,
        [Frequency N1 Count]		AS PowerFrequencyN1,
		CASE [Frequency N Count]
			WHEN 0 THEN NULL
			ELSE [Frequency N1 Count]/[Frequency N Count]
		END AS PowerFrequencyN1Eval,
        [Frequency N2 Count]		AS PowerFrequencyN2,
        CASE [Frequency N Count]
			WHEN 0 THEN NULL
			ELSE [Frequency N2 Count]/[Frequency N Count]
		END							AS PowerFrequencyN2Eval,
		[Frequency N invalid Count]	AS PowerFrequencyNInvalid
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[Frequency N Count],
			[Frequency N1 Count],
			[Frequency N2 Count],
			[Frequency N invalid Count]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID

GO


/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Power Frequency data for the devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160PowerFrequencyDataPL.sql#2 $
******************************************************************************/
ALTER PROCEDURE [dbo].[spDAL_GetEN50160PowerFrequencyDataPL]
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID							INT PRIMARY KEY NOT NULL,
		MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_PowerFrequency TABLE
	(
		TimestampUtc			DATETIME,
		SourceID				INT,
        PowerFrequencyN1Eval	FLOAT,
        PowerFrequencyN2Eval	FLOAT
    )
	 
	-- put the raw data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME,
		Value			FLOAT
	)
	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		s.MeasurementSetIdentifier = UPPER(N'PL')
		AND q.Name IN
		(
			N'Freq N1 Eval',
			N'Freq N2 Eval'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name

	-- select data for the PowerLogic (PL) devices into the results table
	SELECT
		TimestampUtc,
		SourceID,
		[Freq N1 Eval]	AS PowerFrequencyN1Eval,
        [Freq N2 Eval]	AS PowerFrequencyN2Eval
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[Freq N1 Eval],
			[Freq N2 Eval]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID

GO


/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Short and Long Interruptions data for the ION devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160ShortAndLongInterruptionsDataION.sql#2 $
******************************************************************************/
ALTER PROCEDURE dbo.spDAL_GetEN50160ShortAndLongInterruptionsDataION
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID							INT PRIMARY KEY NOT NULL,
		MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_ShortAndLongInterruptions TABLE
	(
		TimestampUtc					DATETIME,
		SourceID						INT,
        Ion_V1VoltageInterruptionsN1	FLOAT,
        Ion_V1VoltageInterruptionsN2	FLOAT,
        Ion_V1VoltageInterruptionsN3	FLOAT,
        Ion_V2VoltageInterruptionsN1	FLOAT,
        Ion_V2VoltageInterruptionsN2	FLOAT,
        Ion_V2VoltageInterruptionsN3	FLOAT,
        Ion_V3VoltageInterruptionsN1	FLOAT,
        Ion_V3VoltageInterruptionsN2	FLOAT,
        Ion_V3VoltageInterruptionsN3	FLOAT
	)
	 
	-- put the raw data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME,
		Value			FLOAT
	)

	-- insert data for the ION devices into the staging table
	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		s.MeasurementSetIdentifier = N'ION'
		AND q.Name IN
		(
			N'Voltage Interruption Count N1 on Input V1',
			N'Voltage Interruption Count N2 on Input V1',
			N'Voltage Interruption Count N3 on Input V1',
			N'Voltage Interruption Count N1 on Input V2',
			N'Voltage Interruption Count N2 on Input V2',
			N'Voltage Interruption Count N3 on Input V2',
			N'Voltage Interruption Count N1 on Input V3',
			N'Voltage Interruption Count N2 on Input V3',
			N'Voltage Interruption Count N3 on Input V3'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name

	-- select data for the ION devices into the results table
	SELECT
		TimestampUtc,
		SourceID,
		[Voltage Interruption Count N1 on Input V1]	AS Ion_V1VoltageInterruptionsN1,
		[Voltage Interruption Count N2 on Input V1]	AS Ion_V1VoltageInterruptionsN2,
		[Voltage Interruption Count N3 on Input V1]	AS Ion_V1VoltageInterruptionsN3,
		[Voltage Interruption Count N1 on Input V2]	AS Ion_V2VoltageInterruptionsN1,
		[Voltage Interruption Count N2 on Input V2]	AS Ion_V2VoltageInterruptionsN2,
		[Voltage Interruption Count N3 on Input V2]	AS Ion_V2VoltageInterruptionsN3,
		[Voltage Interruption Count N1 on Input V3]	AS Ion_V3VoltageInterruptionsN1,
		[Voltage Interruption Count N2 on Input V3]	AS Ion_V3VoltageInterruptionsN2,
		[Voltage Interruption Count N3 on Input V3]	AS Ion_V3VoltageInterruptionsN3
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[Voltage Interruption Count N1 on Input V1],
			[Voltage Interruption Count N2 on Input V1],
			[Voltage Interruption Count N3 on Input V1],
			[Voltage Interruption Count N1 on Input V2],
			[Voltage Interruption Count N2 on Input V2],
			[Voltage Interruption Count N3 on Input V2],
			[Voltage Interruption Count N1 on Input V3],
			[Voltage Interruption Count N2 on Input V3],
			[Voltage Interruption Count N3 on Input V3]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID
GO


/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Short and Long Interruptions data for the PowerLogic devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160ShortAndLongInterruptionsDataPL.sql#2 $
******************************************************************************/
ALTER PROCEDURE dbo.spDAL_GetEN50160ShortAndLongInterruptionsDataPL
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID							INT PRIMARY KEY NOT NULL,
		MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_ShortAndLongInterruptions TABLE
	(
		TimestampUtc					DATETIME,
		SourceID						INT,
        PL_V1VoltageInterruptionsN01	FLOAT,
        PL_V1VoltageInterruptionsN02	FLOAT,
        PL_V1VoltageInterruptionsN03	FLOAT,
        PL_V1VoltageInterruptionsN04	FLOAT,
        PL_V1VoltageInterruptionsN05	FLOAT,
        PL_V1VoltageInterruptionsN06	FLOAT,
        PL_V1VoltageInterruptionsN07	FLOAT,
        PL_V1VoltageInterruptionsN08	FLOAT,
        PL_V1VoltageInterruptionsN09	FLOAT,
        PL_V1VoltageInterruptionsN10	FLOAT,
        PL_V2VoltageInterruptionsN01	FLOAT,
        PL_V2VoltageInterruptionsN02	FLOAT,
        PL_V2VoltageInterruptionsN03	FLOAT,
        PL_V2VoltageInterruptionsN04	FLOAT,
        PL_V2VoltageInterruptionsN05	FLOAT,
        PL_V2VoltageInterruptionsN06	FLOAT,
        PL_V2VoltageInterruptionsN07	FLOAT,
        PL_V2VoltageInterruptionsN08	FLOAT,
        PL_V2VoltageInterruptionsN09	FLOAT,
        PL_V2VoltageInterruptionsN10	FLOAT,
        PL_V3VoltageInterruptionsN01	FLOAT,
        PL_V3VoltageInterruptionsN02	FLOAT,
        PL_V3VoltageInterruptionsN03	FLOAT,
        PL_V3VoltageInterruptionsN04	FLOAT,
        PL_V3VoltageInterruptionsN05	FLOAT,
        PL_V3VoltageInterruptionsN06	FLOAT,
        PL_V3VoltageInterruptionsN07	FLOAT,
        PL_V3VoltageInterruptionsN08	FLOAT,
        PL_V3VoltageInterruptionsN09	FLOAT,
        PL_V3VoltageInterruptionsN10	FLOAT
	)
	 
	-- put the raw data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME,
		Value			FLOAT
	)

	-- insert data for the PowerLogic (PL) devices into the staging table
	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		s.MeasurementSetIdentifier = N'PL'
		AND q.Name IN
		(
			N'V1 Interrupt N01',
			N'V1 Interrupt N02',
			N'V1 Interrupt N03',
			N'V1 Interrupt N04',
			N'V1 Interrupt N05',
			N'V1 Interrupt N06',
			N'V1 Interrupt N07',
			N'V1 Interrupt N08',
			N'V1 Interrupt N09',
			N'V1 Interrupt N10',
			N'V2 Interrupt N01',
			N'V2 Interrupt N02',
			N'V2 Interrupt N03',
			N'V2 Interrupt N04',
			N'V2 Interrupt N05',
			N'V2 Interrupt N06',
			N'V2 Interrupt N07',
			N'V2 Interrupt N08',
			N'V2 Interrupt N09',
			N'V2 Interrupt N10',
			N'V3 Interrupt N01',
			N'V3 Interrupt N02',
			N'V3 Interrupt N03',
			N'V3 Interrupt N04',
			N'V3 Interrupt N05',
			N'V3 Interrupt N06',
			N'V3 Interrupt N07',
			N'V3 Interrupt N08',
			N'V3 Interrupt N09',
			N'V3 Interrupt N10'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name

	-- select data for the PowerLogic (PL) devices into the results table
	SELECT
		TimestampUtc,
		SourceID,
		[V1 Interrupt N01] AS PL_V1VoltageInterruptionsN01,
		[V1 Interrupt N02] AS PL_V1VoltageInterruptionsN02,
		[V1 Interrupt N03] AS PL_V1VoltageInterruptionsN03,
		[V1 Interrupt N04] AS PL_V1VoltageInterruptionsN04,
		[V1 Interrupt N05] AS PL_V1VoltageInterruptionsN05,
		[V1 Interrupt N06] AS PL_V1VoltageInterruptionsN06,
		[V1 Interrupt N07] AS PL_V1VoltageInterruptionsN07,
		[V1 Interrupt N08] AS PL_V1VoltageInterruptionsN08,
		[V1 Interrupt N09] AS PL_V1VoltageInterruptionsN09,
		[V1 Interrupt N10] AS PL_V1VoltageInterruptionsN10,
		[V2 Interrupt N01] AS PL_V2VoltageInterruptionsN01,
		[V2 Interrupt N02] AS PL_V2VoltageInterruptionsN02,
		[V2 Interrupt N03] AS PL_V2VoltageInterruptionsN03,
		[V2 Interrupt N04] AS PL_V2VoltageInterruptionsN04,
		[V2 Interrupt N05] AS PL_V2VoltageInterruptionsN05,
		[V2 Interrupt N06] AS PL_V2VoltageInterruptionsN06,
		[V2 Interrupt N07] AS PL_V2VoltageInterruptionsN07,
		[V2 Interrupt N08] AS PL_V2VoltageInterruptionsN08,
		[V2 Interrupt N09] AS PL_V2VoltageInterruptionsN09,
		[V2 Interrupt N10] AS PL_V2VoltageInterruptionsN10,
		[V3 Interrupt N01] AS PL_V3VoltageInterruptionsN01,
		[V3 Interrupt N02] AS PL_V3VoltageInterruptionsN02,
		[V3 Interrupt N03] AS PL_V3VoltageInterruptionsN03,
		[V3 Interrupt N04] AS PL_V3VoltageInterruptionsN04,
		[V3 Interrupt N05] AS PL_V3VoltageInterruptionsN05,
		[V3 Interrupt N06] AS PL_V3VoltageInterruptionsN06,
		[V3 Interrupt N07] AS PL_V3VoltageInterruptionsN07,
		[V3 Interrupt N08] AS PL_V3VoltageInterruptionsN08,
		[V3 Interrupt N09] AS PL_V3VoltageInterruptionsN09,
		[V3 Interrupt N10] AS PL_V3VoltageInterruptionsN10
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[V1 Interrupt N01],
			[V1 Interrupt N02],
			[V1 Interrupt N03],
			[V1 Interrupt N04],
			[V1 Interrupt N05],
			[V1 Interrupt N06],
			[V1 Interrupt N07],
			[V1 Interrupt N08],
			[V1 Interrupt N09],
			[V1 Interrupt N10],
			[V2 Interrupt N01],
			[V2 Interrupt N02],
			[V2 Interrupt N03],
			[V2 Interrupt N04],
			[V2 Interrupt N05],
			[V2 Interrupt N06],
			[V2 Interrupt N07],
			[V2 Interrupt N08],
			[V2 Interrupt N09],
			[V2 Interrupt N10],
			[V3 Interrupt N01],
			[V3 Interrupt N02],
			[V3 Interrupt N03],
			[V3 Interrupt N04],
			[V3 Interrupt N05],
			[V3 Interrupt N06],
			[V3 Interrupt N07],
			[V3 Interrupt N08],
			[V3 Interrupt N09],
			[V3 Interrupt N10]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID	
GO


/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Supply Voltage Dips data for the ION devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160SupplyVoltageDipsDataION.sql#2 $
******************************************************************************/
ALTER PROCEDURE dbo.spDAL_GetEN50160SupplyVoltageDipsDataION
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID							INT PRIMARY KEY NOT NULL,
		MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_SupplyVoltageDips TABLE
	(
		TimestampUtc				DATETIME,
		SourceID					INT,
        Ion_V1SupplyVoltageDipsN11	FLOAT,
        Ion_V1SupplyVoltageDipsN12	FLOAT,
        Ion_V1SupplyVoltageDipsN13	FLOAT,
        Ion_V1SupplyVoltageDipsN14	FLOAT,
        Ion_V1SupplyVoltageDipsN21	FLOAT,
        Ion_V1SupplyVoltageDipsN22	FLOAT,
        Ion_V1SupplyVoltageDipsN23	FLOAT,
        Ion_V1SupplyVoltageDipsN24	FLOAT,
        Ion_V1SupplyVoltageDipsN31	FLOAT,
        Ion_V1SupplyVoltageDipsN32	FLOAT,
        Ion_V1SupplyVoltageDipsN33	FLOAT,
        Ion_V1SupplyVoltageDipsN34	FLOAT,
        Ion_V1SupplyVoltageDipsN41	FLOAT,
        Ion_V1SupplyVoltageDipsN42	FLOAT,
        Ion_V1SupplyVoltageDipsN43	FLOAT,
        Ion_V1SupplyVoltageDipsN44	FLOAT,
        Ion_V1SupplyVoltageDipsN51	FLOAT,
        Ion_V1SupplyVoltageDipsN52	FLOAT,
        Ion_V1SupplyVoltageDipsN53	FLOAT,
        Ion_V1SupplyVoltageDipsN54	FLOAT,
        Ion_V1SupplyVoltageDipsN61	FLOAT,
        Ion_V1SupplyVoltageDipsN62	FLOAT,
        Ion_V1SupplyVoltageDipsN63	FLOAT,
        Ion_V1SupplyVoltageDipsN64	FLOAT,
        Ion_V2SupplyVoltageDipsN11	FLOAT,
        Ion_V2SupplyVoltageDipsN12	FLOAT,
        Ion_V2SupplyVoltageDipsN13	FLOAT,
        Ion_V2SupplyVoltageDipsN14	FLOAT,
        Ion_V2SupplyVoltageDipsN21	FLOAT,
        Ion_V2SupplyVoltageDipsN22	FLOAT,
        Ion_V2SupplyVoltageDipsN23	FLOAT,
        Ion_V2SupplyVoltageDipsN24	FLOAT,
        Ion_V2SupplyVoltageDipsN31	FLOAT,
        Ion_V2SupplyVoltageDipsN32	FLOAT,
        Ion_V2SupplyVoltageDipsN33	FLOAT,
        Ion_V2SupplyVoltageDipsN34	FLOAT,
        Ion_V2SupplyVoltageDipsN41	FLOAT,
        Ion_V2SupplyVoltageDipsN42	FLOAT,
        Ion_V2SupplyVoltageDipsN43	FLOAT,
        Ion_V2SupplyVoltageDipsN44	FLOAT,
        Ion_V2SupplyVoltageDipsN51	FLOAT,
        Ion_V2SupplyVoltageDipsN52	FLOAT,
        Ion_V2SupplyVoltageDipsN53	FLOAT,
        Ion_V2SupplyVoltageDipsN54	FLOAT,
        Ion_V2SupplyVoltageDipsN61	FLOAT,
        Ion_V2SupplyVoltageDipsN62	FLOAT,
        Ion_V2SupplyVoltageDipsN63	FLOAT,
        Ion_V2SupplyVoltageDipsN64	FLOAT,
        Ion_V3SupplyVoltageDipsN11	FLOAT,
        Ion_V3SupplyVoltageDipsN12	FLOAT,
        Ion_V3SupplyVoltageDipsN13	FLOAT,
        Ion_V3SupplyVoltageDipsN14	FLOAT,
        Ion_V3SupplyVoltageDipsN21	FLOAT,
        Ion_V3SupplyVoltageDipsN22	FLOAT,
        Ion_V3SupplyVoltageDipsN23	FLOAT,
        Ion_V3SupplyVoltageDipsN24	FLOAT,
        Ion_V3SupplyVoltageDipsN31	FLOAT,
        Ion_V3SupplyVoltageDipsN32	FLOAT,
        Ion_V3SupplyVoltageDipsN33	FLOAT,
        Ion_V3SupplyVoltageDipsN34	FLOAT,
        Ion_V3SupplyVoltageDipsN41	FLOAT,
        Ion_V3SupplyVoltageDipsN42	FLOAT,
        Ion_V3SupplyVoltageDipsN43	FLOAT,
        Ion_V3SupplyVoltageDipsN44	FLOAT,
        Ion_V3SupplyVoltageDipsN51	FLOAT,
        Ion_V3SupplyVoltageDipsN52	FLOAT,
        Ion_V3SupplyVoltageDipsN53	FLOAT,
        Ion_V3SupplyVoltageDipsN54	FLOAT,
        Ion_V3SupplyVoltageDipsN61	FLOAT,
        Ion_V3SupplyVoltageDipsN62	FLOAT,
        Ion_V3SupplyVoltageDipsN63	FLOAT,
        Ion_V3SupplyVoltageDipsN64	FLOAT
	)
	 
	-- put the raw data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME,
		Value			FLOAT
	)

	-- insert data for the ION devices into the staging table
	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		s.MeasurementSetIdentifier = N'ION'
		AND q.Name IN
		(
			N'Voltage Dip Count N11 on Input V1',
			N'Voltage Dip Count N12 on Input V1',
			N'Voltage Dip Count N13 on Input V1',
			N'Voltage Dip Count N14 on Input V1',
			N'Voltage Dip Count N21 on Input V1',
			N'Voltage Dip Count N22 on Input V1',
			N'Voltage Dip Count N23 on Input V1',
			N'Voltage Dip Count N24 on Input V1',
			N'Voltage Dip Count N31 on Input V1',
			N'Voltage Dip Count N32 on Input V1',
			N'Voltage Dip Count N33 on Input V1',
			N'Voltage Dip Count N34 on Input V1',
			N'Voltage Dip Count N41 on Input V1',
			N'Voltage Dip Count N42 on Input V1',
			N'Voltage Dip Count N43 on Input V1',
			N'Voltage Dip Count N44 on Input V1',
			N'Voltage Dip Count N51 on Input V1',
			N'Voltage Dip Count N52 on Input V1',
			N'Voltage Dip Count N53 on Input V1',
			N'Voltage Dip Count N54 on Input V1',
			N'Voltage Dip Count N61 on Input V1',
			N'Voltage Dip Count N62 on Input V1',
			N'Voltage Dip Count N63 on Input V1',
			N'Voltage Dip Count N64 on Input V1',
			N'Voltage Dip Count N11 on Input V2',
			N'Voltage Dip Count N12 on Input V2',
			N'Voltage Dip Count N13 on Input V2',
			N'Voltage Dip Count N14 on Input V2',
			N'Voltage Dip Count N21 on Input V2',
			N'Voltage Dip Count N22 on Input V2',
			N'Voltage Dip Count N23 on Input V2',
			N'Voltage Dip Count N24 on Input V2',
			N'Voltage Dip Count N31 on Input V2',
			N'Voltage Dip Count N32 on Input V2',
			N'Voltage Dip Count N33 on Input V2',
			N'Voltage Dip Count N34 on Input V2',
			N'Voltage Dip Count N41 on Input V2',
			N'Voltage Dip Count N42 on Input V2',
			N'Voltage Dip Count N43 on Input V2',
			N'Voltage Dip Count N44 on Input V2',
			N'Voltage Dip Count N51 on Input V2',
			N'Voltage Dip Count N52 on Input V2',
			N'Voltage Dip Count N53 on Input V2',
			N'Voltage Dip Count N54 on Input V2',
			N'Voltage Dip Count N61 on Input V2',
			N'Voltage Dip Count N62 on Input V2',
			N'Voltage Dip Count N63 on Input V2',
			N'Voltage Dip Count N64 on Input V2',
			N'Voltage Dip Count N11 on Input V3',
			N'Voltage Dip Count N12 on Input V3',
			N'Voltage Dip Count N13 on Input V3',
			N'Voltage Dip Count N14 on Input V3',
			N'Voltage Dip Count N21 on Input V3',
			N'Voltage Dip Count N22 on Input V3',
			N'Voltage Dip Count N23 on Input V3',
			N'Voltage Dip Count N24 on Input V3',
			N'Voltage Dip Count N31 on Input V3',
			N'Voltage Dip Count N32 on Input V3',
			N'Voltage Dip Count N33 on Input V3',
			N'Voltage Dip Count N34 on Input V3',
			N'Voltage Dip Count N41 on Input V3',
			N'Voltage Dip Count N42 on Input V3',
			N'Voltage Dip Count N43 on Input V3',
			N'Voltage Dip Count N44 on Input V3',
			N'Voltage Dip Count N51 on Input V3',
			N'Voltage Dip Count N52 on Input V3',
			N'Voltage Dip Count N53 on Input V3',
			N'Voltage Dip Count N54 on Input V3',
			N'Voltage Dip Count N61 on Input V3',
			N'Voltage Dip Count N62 on Input V3',
			N'Voltage Dip Count N63 on Input V3',
			N'Voltage Dip Count N64 on Input V3'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name

	-- select data for the ION devices into the results table
	SELECT
		TimestampUtc,
		SourceID,
		[Voltage Dip Count N11 on Input V1] AS Ion_V1SupplyVoltageDipsN11,
		[Voltage Dip Count N12 on Input V1] AS Ion_V1SupplyVoltageDipsN12,
		[Voltage Dip Count N13 on Input V1] AS Ion_V1SupplyVoltageDipsN13,
		[Voltage Dip Count N14 on Input V1] AS Ion_V1SupplyVoltageDipsN14,
		[Voltage Dip Count N21 on Input V1] AS Ion_V1SupplyVoltageDipsN21,
		[Voltage Dip Count N22 on Input V1] AS Ion_V1SupplyVoltageDipsN22,
		[Voltage Dip Count N23 on Input V1] AS Ion_V1SupplyVoltageDipsN23,
		[Voltage Dip Count N24 on Input V1] AS Ion_V1SupplyVoltageDipsN24,
		[Voltage Dip Count N31 on Input V1] AS Ion_V1SupplyVoltageDipsN31,
		[Voltage Dip Count N32 on Input V1] AS Ion_V1SupplyVoltageDipsN32,
		[Voltage Dip Count N33 on Input V1] AS Ion_V1SupplyVoltageDipsN33,
		[Voltage Dip Count N34 on Input V1] AS Ion_V1SupplyVoltageDipsN34,
		[Voltage Dip Count N41 on Input V1] AS Ion_V1SupplyVoltageDipsN41,
		[Voltage Dip Count N42 on Input V1] AS Ion_V1SupplyVoltageDipsN42,
		[Voltage Dip Count N43 on Input V1] AS Ion_V1SupplyVoltageDipsN43,
		[Voltage Dip Count N44 on Input V1] AS Ion_V1SupplyVoltageDipsN44,
		[Voltage Dip Count N51 on Input V1] AS Ion_V1SupplyVoltageDipsN51,
		[Voltage Dip Count N52 on Input V1] AS Ion_V1SupplyVoltageDipsN52,
		[Voltage Dip Count N53 on Input V1] AS Ion_V1SupplyVoltageDipsN53,
		[Voltage Dip Count N54 on Input V1] AS Ion_V1SupplyVoltageDipsN54,
		[Voltage Dip Count N61 on Input V1] AS Ion_V1SupplyVoltageDipsN61,
		[Voltage Dip Count N62 on Input V1] AS Ion_V1SupplyVoltageDipsN62,
		[Voltage Dip Count N63 on Input V1] AS Ion_V1SupplyVoltageDipsN63,
		[Voltage Dip Count N64 on Input V1] AS Ion_V1SupplyVoltageDipsN64,
		[Voltage Dip Count N11 on Input V2] AS Ion_V2SupplyVoltageDipsN11,
		[Voltage Dip Count N12 on Input V2] AS Ion_V2SupplyVoltageDipsN12,
		[Voltage Dip Count N13 on Input V2] AS Ion_V2SupplyVoltageDipsN13,
		[Voltage Dip Count N14 on Input V2] AS Ion_V2SupplyVoltageDipsN14,
		[Voltage Dip Count N21 on Input V2] AS Ion_V2SupplyVoltageDipsN21,
		[Voltage Dip Count N22 on Input V2] AS Ion_V2SupplyVoltageDipsN22,
		[Voltage Dip Count N23 on Input V2] AS Ion_V2SupplyVoltageDipsN23,
		[Voltage Dip Count N24 on Input V2] AS Ion_V2SupplyVoltageDipsN24,
		[Voltage Dip Count N31 on Input V2] AS Ion_V2SupplyVoltageDipsN31,
		[Voltage Dip Count N32 on Input V2] AS Ion_V2SupplyVoltageDipsN32,
		[Voltage Dip Count N33 on Input V2] AS Ion_V2SupplyVoltageDipsN33,
		[Voltage Dip Count N34 on Input V2] AS Ion_V2SupplyVoltageDipsN34,
		[Voltage Dip Count N41 on Input V2] AS Ion_V2SupplyVoltageDipsN41,
		[Voltage Dip Count N42 on Input V2] AS Ion_V2SupplyVoltageDipsN42,
		[Voltage Dip Count N43 on Input V2] AS Ion_V2SupplyVoltageDipsN43,
		[Voltage Dip Count N44 on Input V2] AS Ion_V2SupplyVoltageDipsN44,
		[Voltage Dip Count N51 on Input V2] AS Ion_V2SupplyVoltageDipsN51,
		[Voltage Dip Count N52 on Input V2] AS Ion_V2SupplyVoltageDipsN52,
		[Voltage Dip Count N53 on Input V2] AS Ion_V2SupplyVoltageDipsN53,
		[Voltage Dip Count N54 on Input V2] AS Ion_V2SupplyVoltageDipsN54,
		[Voltage Dip Count N61 on Input V2] AS Ion_V2SupplyVoltageDipsN61,
		[Voltage Dip Count N62 on Input V2] AS Ion_V2SupplyVoltageDipsN62,
		[Voltage Dip Count N63 on Input V2] AS Ion_V2SupplyVoltageDipsN63,
		[Voltage Dip Count N64 on Input V2] AS Ion_V2SupplyVoltageDipsN64,
		[Voltage Dip Count N11 on Input V3] AS Ion_V3SupplyVoltageDipsN11,
		[Voltage Dip Count N12 on Input V3] AS Ion_V3SupplyVoltageDipsN12,
		[Voltage Dip Count N13 on Input V3] AS Ion_V3SupplyVoltageDipsN13,
		[Voltage Dip Count N14 on Input V3] AS Ion_V3SupplyVoltageDipsN14,
		[Voltage Dip Count N21 on Input V3] AS Ion_V3SupplyVoltageDipsN21,
		[Voltage Dip Count N22 on Input V3] AS Ion_V3SupplyVoltageDipsN22,
		[Voltage Dip Count N23 on Input V3] AS Ion_V3SupplyVoltageDipsN23,
		[Voltage Dip Count N24 on Input V3] AS Ion_V3SupplyVoltageDipsN24,
		[Voltage Dip Count N31 on Input V3] AS Ion_V3SupplyVoltageDipsN31,
		[Voltage Dip Count N32 on Input V3] AS Ion_V3SupplyVoltageDipsN32,
		[Voltage Dip Count N33 on Input V3] AS Ion_V3SupplyVoltageDipsN33,
		[Voltage Dip Count N34 on Input V3] AS Ion_V3SupplyVoltageDipsN34,
		[Voltage Dip Count N41 on Input V3] AS Ion_V3SupplyVoltageDipsN41,
		[Voltage Dip Count N42 on Input V3] AS Ion_V3SupplyVoltageDipsN42,
		[Voltage Dip Count N43 on Input V3] AS Ion_V3SupplyVoltageDipsN43,
		[Voltage Dip Count N44 on Input V3] AS Ion_V3SupplyVoltageDipsN44,
		[Voltage Dip Count N51 on Input V3] AS Ion_V3SupplyVoltageDipsN51,
		[Voltage Dip Count N52 on Input V3] AS Ion_V3SupplyVoltageDipsN52,
		[Voltage Dip Count N53 on Input V3] AS Ion_V3SupplyVoltageDipsN53,
		[Voltage Dip Count N54 on Input V3] AS Ion_V3SupplyVoltageDipsN54,
		[Voltage Dip Count N61 on Input V3] AS Ion_V3SupplyVoltageDipsN61,
		[Voltage Dip Count N62 on Input V3] AS Ion_V3SupplyVoltageDipsN62,
		[Voltage Dip Count N63 on Input V3] AS Ion_V3SupplyVoltageDipsN63,
		[Voltage Dip Count N64 on Input V3] AS Ion_V3SupplyVoltageDipsN64
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[Voltage Dip Count N11 on Input V1],
			[Voltage Dip Count N12 on Input V1],
			[Voltage Dip Count N13 on Input V1],
			[Voltage Dip Count N14 on Input V1],
			[Voltage Dip Count N21 on Input V1],
			[Voltage Dip Count N22 on Input V1],
			[Voltage Dip Count N23 on Input V1],
			[Voltage Dip Count N24 on Input V1],
			[Voltage Dip Count N31 on Input V1],
			[Voltage Dip Count N32 on Input V1],
			[Voltage Dip Count N33 on Input V1],
			[Voltage Dip Count N34 on Input V1],
			[Voltage Dip Count N41 on Input V1],
			[Voltage Dip Count N42 on Input V1],
			[Voltage Dip Count N43 on Input V1],
			[Voltage Dip Count N44 on Input V1],
			[Voltage Dip Count N51 on Input V1],
			[Voltage Dip Count N52 on Input V1],
			[Voltage Dip Count N53 on Input V1],
			[Voltage Dip Count N54 on Input V1],
			[Voltage Dip Count N61 on Input V1],
			[Voltage Dip Count N62 on Input V1],
			[Voltage Dip Count N63 on Input V1],
			[Voltage Dip Count N64 on Input V1],
			[Voltage Dip Count N11 on Input V2],
			[Voltage Dip Count N12 on Input V2],
			[Voltage Dip Count N13 on Input V2],
			[Voltage Dip Count N14 on Input V2],
			[Voltage Dip Count N21 on Input V2],
			[Voltage Dip Count N22 on Input V2],
			[Voltage Dip Count N23 on Input V2],
			[Voltage Dip Count N24 on Input V2],
			[Voltage Dip Count N31 on Input V2],
			[Voltage Dip Count N32 on Input V2],
			[Voltage Dip Count N33 on Input V2],
			[Voltage Dip Count N34 on Input V2],
			[Voltage Dip Count N41 on Input V2],
			[Voltage Dip Count N42 on Input V2],
			[Voltage Dip Count N43 on Input V2],
			[Voltage Dip Count N44 on Input V2],
			[Voltage Dip Count N51 on Input V2],
			[Voltage Dip Count N52 on Input V2],
			[Voltage Dip Count N53 on Input V2],
			[Voltage Dip Count N54 on Input V2],
			[Voltage Dip Count N61 on Input V2],
			[Voltage Dip Count N62 on Input V2],
			[Voltage Dip Count N63 on Input V2],
			[Voltage Dip Count N64 on Input V2],
			[Voltage Dip Count N11 on Input V3],
			[Voltage Dip Count N12 on Input V3],
			[Voltage Dip Count N13 on Input V3],
			[Voltage Dip Count N14 on Input V3],
			[Voltage Dip Count N21 on Input V3],
			[Voltage Dip Count N22 on Input V3],
			[Voltage Dip Count N23 on Input V3],
			[Voltage Dip Count N24 on Input V3],
			[Voltage Dip Count N31 on Input V3],
			[Voltage Dip Count N32 on Input V3],
			[Voltage Dip Count N33 on Input V3],
			[Voltage Dip Count N34 on Input V3],
			[Voltage Dip Count N41 on Input V3],
			[Voltage Dip Count N42 on Input V3],
			[Voltage Dip Count N43 on Input V3],
			[Voltage Dip Count N44 on Input V3],
			[Voltage Dip Count N51 on Input V3],
			[Voltage Dip Count N52 on Input V3],
			[Voltage Dip Count N53 on Input V3],
			[Voltage Dip Count N54 on Input V3],
			[Voltage Dip Count N61 on Input V3],
			[Voltage Dip Count N62 on Input V3],
			[Voltage Dip Count N63 on Input V3],
			[Voltage Dip Count N64 on Input V3]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID
GO


/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Supply Voltage Dips data for the PowerLogic devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160SupplyVoltageDipsDataPL.sql#2 $
******************************************************************************/
ALTER PROCEDURE dbo.spDAL_GetEN50160SupplyVoltageDipsDataPL
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID							INT PRIMARY KEY NOT NULL,
		MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_SupplyVoltageDips TABLE
	(
		TimestampUtc				DATETIME,
		SourceID					INT,
		PL_V1SupplyVoltageDipsN01	FLOAT,
		PL_V1SupplyVoltageDipsN02	FLOAT,
		PL_V1SupplyVoltageDipsN03	FLOAT,
		PL_V1SupplyVoltageDipsN04	FLOAT,
		PL_V1SupplyVoltageDipsN05	FLOAT,
		PL_V1SupplyVoltageDipsN06	FLOAT,
		PL_V1SupplyVoltageDipsN07	FLOAT,
		PL_V1SupplyVoltageDipsN08	FLOAT,
		PL_V1SupplyVoltageDipsN09	FLOAT,
		PL_V1SupplyVoltageDipsN10	FLOAT,
		PL_V1SupplyVoltageDipsN11	FLOAT,
		PL_V1SupplyVoltageDipsN12	FLOAT,
		PL_V1SupplyVoltageDipsN13	FLOAT,
		PL_V1SupplyVoltageDipsN14	FLOAT,
		PL_V1SupplyVoltageDipsN15	FLOAT,
		PL_V1SupplyVoltageDipsN16	FLOAT,
		PL_V1SupplyVoltageDipsN17	FLOAT,
		PL_V1SupplyVoltageDipsN18	FLOAT,
		PL_V1SupplyVoltageDipsN19	FLOAT,
		PL_V1SupplyVoltageDipsN20	FLOAT,
		PL_V1SupplyVoltageDipsN21	FLOAT,
		PL_V1SupplyVoltageDipsN22	FLOAT,
		PL_V1SupplyVoltageDipsN23	FLOAT,
		PL_V1SupplyVoltageDipsN24	FLOAT,
		PL_V1SupplyVoltageDipsN25	FLOAT,
		PL_V1SupplyVoltageDipsN26	FLOAT,
		PL_V1SupplyVoltageDipsN27	FLOAT,
		PL_V1SupplyVoltageDipsN28	FLOAT,
		PL_V1SupplyVoltageDipsN29	FLOAT,
		PL_V1SupplyVoltageDipsN30	FLOAT,
		PL_V1SupplyVoltageDipsN31	FLOAT,
		PL_V1SupplyVoltageDipsN32	FLOAT,
		PL_V1SupplyVoltageDipsN33	FLOAT,
		PL_V1SupplyVoltageDipsN34	FLOAT,
		PL_V1SupplyVoltageDipsN35	FLOAT,
		PL_V1SupplyVoltageDipsN36	FLOAT,
		PL_V1SupplyVoltageDipsN37	FLOAT,
		PL_V1SupplyVoltageDipsN38	FLOAT,
		PL_V1SupplyVoltageDipsN39	FLOAT,
		PL_V1SupplyVoltageDipsN40	FLOAT,
		PL_V1SupplyVoltageDipsN41	FLOAT,
		PL_V1SupplyVoltageDipsN42	FLOAT,
		PL_V1SupplyVoltageDipsN43	FLOAT,
		PL_V1SupplyVoltageDipsN44	FLOAT,
		PL_V1SupplyVoltageDipsN45	FLOAT,
		PL_V1SupplyVoltageDipsN46	FLOAT,
		PL_V1SupplyVoltageDipsN47	FLOAT,
		PL_V1SupplyVoltageDipsN48	FLOAT,
		PL_V1SupplyVoltageDipsN49	FLOAT,
		PL_V1SupplyVoltageDipsN50	FLOAT,
		PL_V1SupplyVoltageDipsN51	FLOAT,
		PL_V1SupplyVoltageDipsN52	FLOAT,
		PL_V1SupplyVoltageDipsN53	FLOAT,
		PL_V1SupplyVoltageDipsN54	FLOAT,
		PL_V1SupplyVoltageDipsN55	FLOAT,
		PL_V1SupplyVoltageDipsN56	FLOAT,
		PL_V1SupplyVoltageDipsN57	FLOAT,
		PL_V1SupplyVoltageDipsN58	FLOAT,
		PL_V1SupplyVoltageDipsN59	FLOAT,
		PL_V1SupplyVoltageDipsN60	FLOAT,
		PL_V1SupplyVoltageDipsN61	FLOAT,
		PL_V1SupplyVoltageDipsN62	FLOAT,
		PL_V1SupplyVoltageDipsN63	FLOAT,
		PL_V1SupplyVoltageDipsN64	FLOAT,
		PL_V1SupplyVoltageDipsN65	FLOAT,
		PL_V1SupplyVoltageDipsN66	FLOAT,
		PL_V1SupplyVoltageDipsN67	FLOAT,
		PL_V1SupplyVoltageDipsN68	FLOAT,
		PL_V1SupplyVoltageDipsN69	FLOAT,
		PL_V1SupplyVoltageDipsN70	FLOAT,
		PL_V1SupplyVoltageDipsN71	FLOAT,
		PL_V1SupplyVoltageDipsN72	FLOAT,
		PL_V1SupplyVoltageDipsN73	FLOAT,
		PL_V1SupplyVoltageDipsN74	FLOAT,
		PL_V1SupplyVoltageDipsN75	FLOAT,
		PL_V1SupplyVoltageDipsN76	FLOAT,
		PL_V1SupplyVoltageDipsN77	FLOAT,
		PL_V1SupplyVoltageDipsN78	FLOAT,
		PL_V1SupplyVoltageDipsN79	FLOAT,
		PL_V1SupplyVoltageDipsN80	FLOAT,
		PL_V1SupplyVoltageDipsN81	FLOAT,
		PL_V1SupplyVoltageDipsN82	FLOAT,
		PL_V1SupplyVoltageDipsN83	FLOAT,
		PL_V1SupplyVoltageDipsN84	FLOAT,
		PL_V1SupplyVoltageDipsN85	FLOAT,
		PL_V1SupplyVoltageDipsN86	FLOAT,
		PL_V1SupplyVoltageDipsN87	FLOAT,
		PL_V1SupplyVoltageDipsN88	FLOAT,
		PL_V1SupplyVoltageDipsN89	FLOAT,
		PL_V1SupplyVoltageDipsN90	FLOAT,
		PL_V1SupplyVoltageDipsN91	FLOAT,
		PL_V1SupplyVoltageDipsN92	FLOAT,
		PL_V1SupplyVoltageDipsN93	FLOAT,
		PL_V1SupplyVoltageDipsN94	FLOAT,
		PL_V1SupplyVoltageDipsN95	FLOAT,
		PL_V1SupplyVoltageDipsN96	FLOAT,
		PL_V2SupplyVoltageDipsN01	FLOAT,
		PL_V2SupplyVoltageDipsN02	FLOAT,
		PL_V2SupplyVoltageDipsN03	FLOAT,
		PL_V2SupplyVoltageDipsN04	FLOAT,
		PL_V2SupplyVoltageDipsN05	FLOAT,
		PL_V2SupplyVoltageDipsN06	FLOAT,
		PL_V2SupplyVoltageDipsN07	FLOAT,
		PL_V2SupplyVoltageDipsN08	FLOAT,
		PL_V2SupplyVoltageDipsN09	FLOAT,
		PL_V2SupplyVoltageDipsN10	FLOAT,
		PL_V2SupplyVoltageDipsN11	FLOAT,
		PL_V2SupplyVoltageDipsN12	FLOAT,
		PL_V2SupplyVoltageDipsN13	FLOAT,
		PL_V2SupplyVoltageDipsN14	FLOAT,
		PL_V2SupplyVoltageDipsN15	FLOAT,
		PL_V2SupplyVoltageDipsN16	FLOAT,
		PL_V2SupplyVoltageDipsN17	FLOAT,
		PL_V2SupplyVoltageDipsN18	FLOAT,
		PL_V2SupplyVoltageDipsN19	FLOAT,
		PL_V2SupplyVoltageDipsN20	FLOAT,
		PL_V2SupplyVoltageDipsN21	FLOAT,
		PL_V2SupplyVoltageDipsN22	FLOAT,
		PL_V2SupplyVoltageDipsN23	FLOAT,
		PL_V2SupplyVoltageDipsN24	FLOAT,
		PL_V2SupplyVoltageDipsN25	FLOAT,
		PL_V2SupplyVoltageDipsN26	FLOAT,
		PL_V2SupplyVoltageDipsN27	FLOAT,
		PL_V2SupplyVoltageDipsN28	FLOAT,
		PL_V2SupplyVoltageDipsN29	FLOAT,
		PL_V2SupplyVoltageDipsN30	FLOAT,
		PL_V2SupplyVoltageDipsN31	FLOAT,
		PL_V2SupplyVoltageDipsN32	FLOAT,
		PL_V2SupplyVoltageDipsN33	FLOAT,
		PL_V2SupplyVoltageDipsN34	FLOAT,
		PL_V2SupplyVoltageDipsN35	FLOAT,
		PL_V2SupplyVoltageDipsN36	FLOAT,
		PL_V2SupplyVoltageDipsN37	FLOAT,
		PL_V2SupplyVoltageDipsN38	FLOAT,
		PL_V2SupplyVoltageDipsN39	FLOAT,
		PL_V2SupplyVoltageDipsN40	FLOAT,
		PL_V2SupplyVoltageDipsN41	FLOAT,
		PL_V2SupplyVoltageDipsN42	FLOAT,
		PL_V2SupplyVoltageDipsN43	FLOAT,
		PL_V2SupplyVoltageDipsN44	FLOAT,
		PL_V2SupplyVoltageDipsN45	FLOAT,
		PL_V2SupplyVoltageDipsN46	FLOAT,
		PL_V2SupplyVoltageDipsN47	FLOAT,
		PL_V2SupplyVoltageDipsN48	FLOAT,
		PL_V2SupplyVoltageDipsN49	FLOAT,
		PL_V2SupplyVoltageDipsN50	FLOAT,
		PL_V2SupplyVoltageDipsN51	FLOAT,
		PL_V2SupplyVoltageDipsN52	FLOAT,
		PL_V2SupplyVoltageDipsN53	FLOAT,
		PL_V2SupplyVoltageDipsN54	FLOAT,
		PL_V2SupplyVoltageDipsN55	FLOAT,
		PL_V2SupplyVoltageDipsN56	FLOAT,
		PL_V2SupplyVoltageDipsN57	FLOAT,
		PL_V2SupplyVoltageDipsN58	FLOAT,
		PL_V2SupplyVoltageDipsN59	FLOAT,
		PL_V2SupplyVoltageDipsN60	FLOAT,
		PL_V2SupplyVoltageDipsN61	FLOAT,
		PL_V2SupplyVoltageDipsN62	FLOAT,
		PL_V2SupplyVoltageDipsN63	FLOAT,
		PL_V2SupplyVoltageDipsN64	FLOAT,
		PL_V2SupplyVoltageDipsN65	FLOAT,
		PL_V2SupplyVoltageDipsN66	FLOAT,
		PL_V2SupplyVoltageDipsN67	FLOAT,
		PL_V2SupplyVoltageDipsN68	FLOAT,
		PL_V2SupplyVoltageDipsN69	FLOAT,
		PL_V2SupplyVoltageDipsN70	FLOAT,
		PL_V2SupplyVoltageDipsN71	FLOAT,
		PL_V2SupplyVoltageDipsN72	FLOAT,
		PL_V2SupplyVoltageDipsN73	FLOAT,
		PL_V2SupplyVoltageDipsN74	FLOAT,
		PL_V2SupplyVoltageDipsN75	FLOAT,
		PL_V2SupplyVoltageDipsN76	FLOAT,
		PL_V2SupplyVoltageDipsN77	FLOAT,
		PL_V2SupplyVoltageDipsN78	FLOAT,
		PL_V2SupplyVoltageDipsN79	FLOAT,
		PL_V2SupplyVoltageDipsN80	FLOAT,
		PL_V2SupplyVoltageDipsN81	FLOAT,
		PL_V2SupplyVoltageDipsN82	FLOAT,
		PL_V2SupplyVoltageDipsN83	FLOAT,
		PL_V2SupplyVoltageDipsN84	FLOAT,
		PL_V2SupplyVoltageDipsN85	FLOAT,
		PL_V2SupplyVoltageDipsN86	FLOAT,
		PL_V2SupplyVoltageDipsN87	FLOAT,
		PL_V2SupplyVoltageDipsN88	FLOAT,
		PL_V2SupplyVoltageDipsN89	FLOAT,
		PL_V2SupplyVoltageDipsN90	FLOAT,
		PL_V2SupplyVoltageDipsN91	FLOAT,
		PL_V2SupplyVoltageDipsN92	FLOAT,
		PL_V2SupplyVoltageDipsN93	FLOAT,
		PL_V2SupplyVoltageDipsN94	FLOAT,
		PL_V2SupplyVoltageDipsN95	FLOAT,
		PL_V2SupplyVoltageDipsN96	FLOAT,
		PL_V3SupplyVoltageDipsN01	FLOAT,
		PL_V3SupplyVoltageDipsN02	FLOAT,
		PL_V3SupplyVoltageDipsN03	FLOAT,
		PL_V3SupplyVoltageDipsN04	FLOAT,
		PL_V3SupplyVoltageDipsN05	FLOAT,
		PL_V3SupplyVoltageDipsN06	FLOAT,
		PL_V3SupplyVoltageDipsN07	FLOAT,
		PL_V3SupplyVoltageDipsN08	FLOAT,
		PL_V3SupplyVoltageDipsN09	FLOAT,
		PL_V3SupplyVoltageDipsN10	FLOAT,
		PL_V3SupplyVoltageDipsN11	FLOAT,
		PL_V3SupplyVoltageDipsN12	FLOAT,
		PL_V3SupplyVoltageDipsN13	FLOAT,
		PL_V3SupplyVoltageDipsN14	FLOAT,
		PL_V3SupplyVoltageDipsN15	FLOAT,
		PL_V3SupplyVoltageDipsN16	FLOAT,
		PL_V3SupplyVoltageDipsN17	FLOAT,
		PL_V3SupplyVoltageDipsN18	FLOAT,
		PL_V3SupplyVoltageDipsN19	FLOAT,
		PL_V3SupplyVoltageDipsN20	FLOAT,
		PL_V3SupplyVoltageDipsN21	FLOAT,
		PL_V3SupplyVoltageDipsN22	FLOAT,
		PL_V3SupplyVoltageDipsN23	FLOAT,
		PL_V3SupplyVoltageDipsN24	FLOAT,
		PL_V3SupplyVoltageDipsN25	FLOAT,
		PL_V3SupplyVoltageDipsN26	FLOAT,
		PL_V3SupplyVoltageDipsN27	FLOAT,
		PL_V3SupplyVoltageDipsN28	FLOAT,
		PL_V3SupplyVoltageDipsN29	FLOAT,
		PL_V3SupplyVoltageDipsN30	FLOAT,
		PL_V3SupplyVoltageDipsN31	FLOAT,
		PL_V3SupplyVoltageDipsN32	FLOAT,
		PL_V3SupplyVoltageDipsN33	FLOAT,
		PL_V3SupplyVoltageDipsN34	FLOAT,
		PL_V3SupplyVoltageDipsN35	FLOAT,
		PL_V3SupplyVoltageDipsN36	FLOAT,
		PL_V3SupplyVoltageDipsN37	FLOAT,
		PL_V3SupplyVoltageDipsN38	FLOAT,
		PL_V3SupplyVoltageDipsN39	FLOAT,
		PL_V3SupplyVoltageDipsN40	FLOAT,
		PL_V3SupplyVoltageDipsN41	FLOAT,
		PL_V3SupplyVoltageDipsN42	FLOAT,
		PL_V3SupplyVoltageDipsN43	FLOAT,
		PL_V3SupplyVoltageDipsN44	FLOAT,
		PL_V3SupplyVoltageDipsN45	FLOAT,
		PL_V3SupplyVoltageDipsN46	FLOAT,
		PL_V3SupplyVoltageDipsN47	FLOAT,
		PL_V3SupplyVoltageDipsN48	FLOAT,
		PL_V3SupplyVoltageDipsN49	FLOAT,
		PL_V3SupplyVoltageDipsN50	FLOAT,
		PL_V3SupplyVoltageDipsN51	FLOAT,
		PL_V3SupplyVoltageDipsN52	FLOAT,
		PL_V3SupplyVoltageDipsN53	FLOAT,
		PL_V3SupplyVoltageDipsN54	FLOAT,
		PL_V3SupplyVoltageDipsN55	FLOAT,
		PL_V3SupplyVoltageDipsN56	FLOAT,
		PL_V3SupplyVoltageDipsN57	FLOAT,
		PL_V3SupplyVoltageDipsN58	FLOAT,
		PL_V3SupplyVoltageDipsN59	FLOAT,
		PL_V3SupplyVoltageDipsN60	FLOAT,
		PL_V3SupplyVoltageDipsN61	FLOAT,
		PL_V3SupplyVoltageDipsN62	FLOAT,
		PL_V3SupplyVoltageDipsN63	FLOAT,
		PL_V3SupplyVoltageDipsN64	FLOAT,
		PL_V3SupplyVoltageDipsN65	FLOAT,
		PL_V3SupplyVoltageDipsN66	FLOAT,
		PL_V3SupplyVoltageDipsN67	FLOAT,
		PL_V3SupplyVoltageDipsN68	FLOAT,
		PL_V3SupplyVoltageDipsN69	FLOAT,
		PL_V3SupplyVoltageDipsN70	FLOAT,
		PL_V3SupplyVoltageDipsN71	FLOAT,
		PL_V3SupplyVoltageDipsN72	FLOAT,
		PL_V3SupplyVoltageDipsN73	FLOAT,
		PL_V3SupplyVoltageDipsN74	FLOAT,
		PL_V3SupplyVoltageDipsN75	FLOAT,
		PL_V3SupplyVoltageDipsN76	FLOAT,
		PL_V3SupplyVoltageDipsN77	FLOAT,
		PL_V3SupplyVoltageDipsN78	FLOAT,
		PL_V3SupplyVoltageDipsN79	FLOAT,
		PL_V3SupplyVoltageDipsN80	FLOAT,
		PL_V3SupplyVoltageDipsN81	FLOAT,
		PL_V3SupplyVoltageDipsN82	FLOAT,
		PL_V3SupplyVoltageDipsN83	FLOAT,
		PL_V3SupplyVoltageDipsN84	FLOAT,
		PL_V3SupplyVoltageDipsN85	FLOAT,
		PL_V3SupplyVoltageDipsN86	FLOAT,
		PL_V3SupplyVoltageDipsN87	FLOAT,
		PL_V3SupplyVoltageDipsN88	FLOAT,
		PL_V3SupplyVoltageDipsN89	FLOAT,
		PL_V3SupplyVoltageDipsN90	FLOAT,
		PL_V3SupplyVoltageDipsN91	FLOAT,
		PL_V3SupplyVoltageDipsN92	FLOAT,
		PL_V3SupplyVoltageDipsN93	FLOAT,
		PL_V3SupplyVoltageDipsN94	FLOAT,
		PL_V3SupplyVoltageDipsN95	FLOAT,
		PL_V3SupplyVoltageDipsN96	FLOAT
	)
	 
	-- put the raw data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME,
		Value			FLOAT
	)

	-- insert data for the PowerLogic (PL) devices into the staging table
	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		s.MeasurementSetIdentifier = N'PL'
		AND q.Name IN
		(
			N'V1 Dip N01',
			N'V1 Dip N02',
			N'V1 Dip N03',
			N'V1 Dip N04',
			N'V1 Dip N05',
			N'V1 Dip N06',
			N'V1 Dip N07',
			N'V1 Dip N08',
			N'V1 Dip N09',
			N'V1 Dip N10',
			N'V1 Dip N11',
			N'V1 Dip N12',
			N'V1 Dip N13',
			N'V1 Dip N14',
			N'V1 Dip N15',
			N'V1 Dip N16',
			N'V1 Dip N17',
			N'V1 Dip N18',
			N'V1 Dip N19',
			N'V1 Dip N20',
			N'V1 Dip N21',
			N'V1 Dip N22',
			N'V1 Dip N23',
			N'V1 Dip N24',
			N'V1 Dip N25',
			N'V1 Dip N26',
			N'V1 Dip N27',
			N'V1 Dip N28',
			N'V1 Dip N29',
			N'V1 Dip N30',
			N'V1 Dip N31',
			N'V1 Dip N32',
			N'V1 Dip N33',
			N'V1 Dip N34',
			N'V1 Dip N35',
			N'V1 Dip N36',
			N'V1 Dip N37',
			N'V1 Dip N38',
			N'V1 Dip N39',
			N'V1 Dip N40',
			N'V1 Dip N41',
			N'V1 Dip N42',
			N'V1 Dip N43',
			N'V1 Dip N44',
			N'V1 Dip N45',
			N'V1 Dip N46',
			N'V1 Dip N47',
			N'V1 Dip N48',
			N'V1 Dip N49',
			N'V1 Dip N50',
			N'V1 Dip N51',
			N'V1 Dip N52',
			N'V1 Dip N53',
			N'V1 Dip N54',
			N'V1 Dip N55',
			N'V1 Dip N56',
			N'V1 Dip N57',
			N'V1 Dip N58',
			N'V1 Dip N59',
			N'V1 Dip N60',
			N'V1 Dip N61',
			N'V1 Dip N62',
			N'V1 Dip N63',
			N'V1 Dip N64',
			N'V1 Dip N65',
			N'V1 Dip N66',
			N'V1 Dip N67',
			N'V1 Dip N68',
			N'V1 Dip N69',
			N'V1 Dip N70',
			N'V1 Dip N71',
			N'V1 Dip N72',
			N'V1 Dip N73',
			N'V1 Dip N74',
			N'V1 Dip N75',
			N'V1 Dip N76',
			N'V1 Dip N77',
			N'V1 Dip N78',
			N'V1 Dip N79',
			N'V1 Dip N80',
			N'V1 Dip N81',
			N'V1 Dip N82',
			N'V1 Dip N83',
			N'V1 Dip N84',
			N'V1 Dip N85',
			N'V1 Dip N86',
			N'V1 Dip N87',
			N'V1 Dip N88',
			N'V1 Dip N89',
			N'V1 Dip N90',
			N'V1 Dip N91',
			N'V1 Dip N92',
			N'V1 Dip N93',
			N'V1 Dip N94',
			N'V1 Dip N95',
			N'V1 Dip N96',
			N'V2 Dip N01',
			N'V2 Dip N02',
			N'V2 Dip N03',
			N'V2 Dip N04',
			N'V2 Dip N05',
			N'V2 Dip N06',
			N'V2 Dip N07',
			N'V2 Dip N08',
			N'V2 Dip N09',
			N'V2 Dip N10',
			N'V2 Dip N11',
			N'V2 Dip N12',
			N'V2 Dip N13',
			N'V2 Dip N14',
			N'V2 Dip N15',
			N'V2 Dip N16',
			N'V2 Dip N17',
			N'V2 Dip N18',
			N'V2 Dip N19',
			N'V2 Dip N20',
			N'V2 Dip N21',
			N'V2 Dip N22',
			N'V2 Dip N23',
			N'V2 Dip N24',
			N'V2 Dip N25',
			N'V2 Dip N26',
			N'V2 Dip N27',
			N'V2 Dip N28',
			N'V2 Dip N29',
			N'V2 Dip N30',
			N'V2 Dip N31',
			N'V2 Dip N32',
			N'V2 Dip N33',
			N'V2 Dip N34',
			N'V2 Dip N35',
			N'V2 Dip N36',
			N'V2 Dip N37',
			N'V2 Dip N38',
			N'V2 Dip N39',
			N'V2 Dip N40',
			N'V2 Dip N41',
			N'V2 Dip N42',
			N'V2 Dip N43',
			N'V2 Dip N44',
			N'V2 Dip N45',
			N'V2 Dip N46',
			N'V2 Dip N47',
			N'V2 Dip N48',
			N'V2 Dip N49',
			N'V2 Dip N50',
			N'V2 Dip N51',
			N'V2 Dip N52',
			N'V2 Dip N53',
			N'V2 Dip N54',
			N'V2 Dip N55',
			N'V2 Dip N56',
			N'V2 Dip N57',
			N'V2 Dip N58',
			N'V2 Dip N59',
			N'V2 Dip N60',
			N'V2 Dip N61',
			N'V2 Dip N62',
			N'V2 Dip N63',
			N'V2 Dip N64',
			N'V2 Dip N65',
			N'V2 Dip N66',
			N'V2 Dip N67',
			N'V2 Dip N68',
			N'V2 Dip N69',
			N'V2 Dip N70',
			N'V2 Dip N71',
			N'V2 Dip N72',
			N'V2 Dip N73',
			N'V2 Dip N74',
			N'V2 Dip N75',
			N'V2 Dip N76',
			N'V2 Dip N77',
			N'V2 Dip N78',
			N'V2 Dip N79',
			N'V2 Dip N80',
			N'V2 Dip N81',
			N'V2 Dip N82',
			N'V2 Dip N83',
			N'V2 Dip N84',
			N'V2 Dip N85',
			N'V2 Dip N86',
			N'V2 Dip N87',
			N'V2 Dip N88',
			N'V2 Dip N89',
			N'V2 Dip N90',
			N'V2 Dip N91',
			N'V2 Dip N92',
			N'V2 Dip N93',
			N'V2 Dip N94',
			N'V2 Dip N95',
			N'V2 Dip N96',
			N'V3 Dip N01',
			N'V3 Dip N02',
			N'V3 Dip N03',
			N'V3 Dip N04',
			N'V3 Dip N05',
			N'V3 Dip N06',
			N'V3 Dip N07',
			N'V3 Dip N08',
			N'V3 Dip N09',
			N'V3 Dip N10',
			N'V3 Dip N11',
			N'V3 Dip N12',
			N'V3 Dip N13',
			N'V3 Dip N14',
			N'V3 Dip N15',
			N'V3 Dip N16',
			N'V3 Dip N17',
			N'V3 Dip N18',
			N'V3 Dip N19',
			N'V3 Dip N20',
			N'V3 Dip N21',
			N'V3 Dip N22',
			N'V3 Dip N23',
			N'V3 Dip N24',
			N'V3 Dip N25',
			N'V3 Dip N26',
			N'V3 Dip N27',
			N'V3 Dip N28',
			N'V3 Dip N29',
			N'V3 Dip N30',
			N'V3 Dip N31',
			N'V3 Dip N32',
			N'V3 Dip N33',
			N'V3 Dip N34',
			N'V3 Dip N35',
			N'V3 Dip N36',
			N'V3 Dip N37',
			N'V3 Dip N38',
			N'V3 Dip N39',
			N'V3 Dip N40',
			N'V3 Dip N41',
			N'V3 Dip N42',
			N'V3 Dip N43',
			N'V3 Dip N44',
			N'V3 Dip N45',
			N'V3 Dip N46',
			N'V3 Dip N47',
			N'V3 Dip N48',
			N'V3 Dip N49',
			N'V3 Dip N50',
			N'V3 Dip N51',
			N'V3 Dip N52',
			N'V3 Dip N53',
			N'V3 Dip N54',
			N'V3 Dip N55',
			N'V3 Dip N56',
			N'V3 Dip N57',
			N'V3 Dip N58',
			N'V3 Dip N59',
			N'V3 Dip N60',
			N'V3 Dip N61',
			N'V3 Dip N62',
			N'V3 Dip N63',
			N'V3 Dip N64',
			N'V3 Dip N65',
			N'V3 Dip N66',
			N'V3 Dip N67',
			N'V3 Dip N68',
			N'V3 Dip N69',
			N'V3 Dip N70',
			N'V3 Dip N71',
			N'V3 Dip N72',
			N'V3 Dip N73',
			N'V3 Dip N74',
			N'V3 Dip N75',
			N'V3 Dip N76',
			N'V3 Dip N77',
			N'V3 Dip N78',
			N'V3 Dip N79',
			N'V3 Dip N80',
			N'V3 Dip N81',
			N'V3 Dip N82',
			N'V3 Dip N83',
			N'V3 Dip N84',
			N'V3 Dip N85',
			N'V3 Dip N86',
			N'V3 Dip N87',
			N'V3 Dip N88',
			N'V3 Dip N89',
			N'V3 Dip N90',
			N'V3 Dip N91',
			N'V3 Dip N92',
			N'V3 Dip N93',
			N'V3 Dip N94',
			N'V3 Dip N95',
			N'V3 Dip N96'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name

	-- select data for the PowerLogic (PL) devices into the results table
	SELECT
		TimestampUtc,
		SourceID,
		[V1 Dip N01] AS PL_V1SupplyVoltageDipsN01,
		[V1 Dip N02] AS PL_V1SupplyVoltageDipsN02,
		[V1 Dip N03] AS PL_V1SupplyVoltageDipsN03,
		[V1 Dip N04] AS PL_V1SupplyVoltageDipsN04,
		[V1 Dip N05] AS PL_V1SupplyVoltageDipsN05,
		[V1 Dip N06] AS PL_V1SupplyVoltageDipsN06,
		[V1 Dip N07] AS PL_V1SupplyVoltageDipsN07,
		[V1 Dip N08] AS PL_V1SupplyVoltageDipsN08,
		[V1 Dip N09] AS PL_V1SupplyVoltageDipsN09,
		[V1 Dip N10] AS PL_V1SupplyVoltageDipsN10,
		[V1 Dip N11] AS PL_V1SupplyVoltageDipsN11,
		[V1 Dip N12] AS PL_V1SupplyVoltageDipsN12,
		[V1 Dip N13] AS PL_V1SupplyVoltageDipsN13,
		[V1 Dip N14] AS PL_V1SupplyVoltageDipsN14,
		[V1 Dip N15] AS PL_V1SupplyVoltageDipsN15,
		[V1 Dip N16] AS PL_V1SupplyVoltageDipsN16,
		[V1 Dip N17] AS PL_V1SupplyVoltageDipsN17,
		[V1 Dip N18] AS PL_V1SupplyVoltageDipsN18,
		[V1 Dip N19] AS PL_V1SupplyVoltageDipsN19,
		[V1 Dip N20] AS PL_V1SupplyVoltageDipsN20,
		[V1 Dip N21] AS PL_V1SupplyVoltageDipsN21,
		[V1 Dip N22] AS PL_V1SupplyVoltageDipsN22,
		[V1 Dip N23] AS PL_V1SupplyVoltageDipsN23,
		[V1 Dip N24] AS PL_V1SupplyVoltageDipsN24,
		[V1 Dip N25] AS PL_V1SupplyVoltageDipsN25,
		[V1 Dip N26] AS PL_V1SupplyVoltageDipsN26,
		[V1 Dip N27] AS PL_V1SupplyVoltageDipsN27,
		[V1 Dip N28] AS PL_V1SupplyVoltageDipsN28,
		[V1 Dip N29] AS PL_V1SupplyVoltageDipsN29,
		[V1 Dip N30] AS PL_V1SupplyVoltageDipsN30,
		[V1 Dip N31] AS PL_V1SupplyVoltageDipsN31,
		[V1 Dip N32] AS PL_V1SupplyVoltageDipsN32,
		[V1 Dip N33] AS PL_V1SupplyVoltageDipsN33,
		[V1 Dip N34] AS PL_V1SupplyVoltageDipsN34,
		[V1 Dip N35] AS PL_V1SupplyVoltageDipsN35,
		[V1 Dip N36] AS PL_V1SupplyVoltageDipsN36,
		[V1 Dip N37] AS PL_V1SupplyVoltageDipsN37,
		[V1 Dip N38] AS PL_V1SupplyVoltageDipsN38,
		[V1 Dip N39] AS PL_V1SupplyVoltageDipsN39,
		[V1 Dip N40] AS PL_V1SupplyVoltageDipsN40,
		[V1 Dip N41] AS PL_V1SupplyVoltageDipsN41,
		[V1 Dip N42] AS PL_V1SupplyVoltageDipsN42,
		[V1 Dip N43] AS PL_V1SupplyVoltageDipsN43,
		[V1 Dip N44] AS PL_V1SupplyVoltageDipsN44,
		[V1 Dip N45] AS PL_V1SupplyVoltageDipsN45,
		[V1 Dip N46] AS PL_V1SupplyVoltageDipsN46,
		[V1 Dip N47] AS PL_V1SupplyVoltageDipsN47,
		[V1 Dip N48] AS PL_V1SupplyVoltageDipsN48,
		[V1 Dip N49] AS PL_V1SupplyVoltageDipsN49,
		[V1 Dip N50] AS PL_V1SupplyVoltageDipsN50,
		[V1 Dip N51] AS PL_V1SupplyVoltageDipsN51,
		[V1 Dip N52] AS PL_V1SupplyVoltageDipsN52,
		[V1 Dip N53] AS PL_V1SupplyVoltageDipsN53,
		[V1 Dip N54] AS PL_V1SupplyVoltageDipsN54,
		[V1 Dip N55] AS PL_V1SupplyVoltageDipsN55,
		[V1 Dip N56] AS PL_V1SupplyVoltageDipsN56,
		[V1 Dip N57] AS PL_V1SupplyVoltageDipsN57,
		[V1 Dip N58] AS PL_V1SupplyVoltageDipsN58,
		[V1 Dip N59] AS PL_V1SupplyVoltageDipsN59,
		[V1 Dip N60] AS PL_V1SupplyVoltageDipsN60,
		[V1 Dip N61] AS PL_V1SupplyVoltageDipsN61,
		[V1 Dip N62] AS PL_V1SupplyVoltageDipsN62,
		[V1 Dip N63] AS PL_V1SupplyVoltageDipsN63,
		[V1 Dip N64] AS PL_V1SupplyVoltageDipsN64,
		[V1 Dip N65] AS PL_V1SupplyVoltageDipsN65,
		[V1 Dip N66] AS PL_V1SupplyVoltageDipsN66,
		[V1 Dip N67] AS PL_V1SupplyVoltageDipsN67,
		[V1 Dip N68] AS PL_V1SupplyVoltageDipsN68,
		[V1 Dip N69] AS PL_V1SupplyVoltageDipsN69,
		[V1 Dip N70] AS PL_V1SupplyVoltageDipsN70,
		[V1 Dip N71] AS PL_V1SupplyVoltageDipsN71,
		[V1 Dip N72] AS PL_V1SupplyVoltageDipsN72,
		[V1 Dip N73] AS PL_V1SupplyVoltageDipsN73,
		[V1 Dip N74] AS PL_V1SupplyVoltageDipsN74,
		[V1 Dip N75] AS PL_V1SupplyVoltageDipsN75,
		[V1 Dip N76] AS PL_V1SupplyVoltageDipsN76,
		[V1 Dip N77] AS PL_V1SupplyVoltageDipsN77,
		[V1 Dip N78] AS PL_V1SupplyVoltageDipsN78,
		[V1 Dip N79] AS PL_V1SupplyVoltageDipsN79,
		[V1 Dip N80] AS PL_V1SupplyVoltageDipsN80,
		[V1 Dip N81] AS PL_V1SupplyVoltageDipsN81,
		[V1 Dip N82] AS PL_V1SupplyVoltageDipsN82,
		[V1 Dip N83] AS PL_V1SupplyVoltageDipsN83,
		[V1 Dip N84] AS PL_V1SupplyVoltageDipsN84,
		[V1 Dip N85] AS PL_V1SupplyVoltageDipsN85,
		[V1 Dip N86] AS PL_V1SupplyVoltageDipsN86,
		[V1 Dip N87] AS PL_V1SupplyVoltageDipsN87,
		[V1 Dip N88] AS PL_V1SupplyVoltageDipsN88,
		[V1 Dip N89] AS PL_V1SupplyVoltageDipsN89,
		[V1 Dip N90] AS PL_V1SupplyVoltageDipsN90,
		[V1 Dip N91] AS PL_V1SupplyVoltageDipsN91,
		[V1 Dip N92] AS PL_V1SupplyVoltageDipsN92,
		[V1 Dip N93] AS PL_V1SupplyVoltageDipsN93,
		[V1 Dip N94] AS PL_V1SupplyVoltageDipsN94,
		[V1 Dip N95] AS PL_V1SupplyVoltageDipsN95,
		[V1 Dip N96] AS PL_V1SupplyVoltageDipsN96,
		[V2 Dip N01] AS PL_V2SupplyVoltageDipsN01,
		[V2 Dip N02] AS PL_V2SupplyVoltageDipsN02,
		[V2 Dip N03] AS PL_V2SupplyVoltageDipsN03,
		[V2 Dip N04] AS PL_V2SupplyVoltageDipsN04,
		[V2 Dip N05] AS PL_V2SupplyVoltageDipsN05,
		[V2 Dip N06] AS PL_V2SupplyVoltageDipsN06,
		[V2 Dip N07] AS PL_V2SupplyVoltageDipsN07,
		[V2 Dip N08] AS PL_V2SupplyVoltageDipsN08,
		[V2 Dip N09] AS PL_V2SupplyVoltageDipsN09,
		[V2 Dip N10] AS PL_V2SupplyVoltageDipsN10,
		[V2 Dip N11] AS PL_V2SupplyVoltageDipsN11,
		[V2 Dip N12] AS PL_V2SupplyVoltageDipsN12,
		[V2 Dip N13] AS PL_V2SupplyVoltageDipsN13,
		[V2 Dip N14] AS PL_V2SupplyVoltageDipsN14,
		[V2 Dip N15] AS PL_V2SupplyVoltageDipsN15,
		[V2 Dip N16] AS PL_V2SupplyVoltageDipsN16,
		[V2 Dip N17] AS PL_V2SupplyVoltageDipsN17,
		[V2 Dip N18] AS PL_V2SupplyVoltageDipsN18,
		[V2 Dip N19] AS PL_V2SupplyVoltageDipsN19,
		[V2 Dip N20] AS PL_V2SupplyVoltageDipsN20,
		[V2 Dip N21] AS PL_V2SupplyVoltageDipsN21,
		[V2 Dip N22] AS PL_V2SupplyVoltageDipsN22,
		[V2 Dip N23] AS PL_V2SupplyVoltageDipsN23,
		[V2 Dip N24] AS PL_V2SupplyVoltageDipsN24,
		[V2 Dip N25] AS PL_V2SupplyVoltageDipsN25,
		[V2 Dip N26] AS PL_V2SupplyVoltageDipsN26,
		[V2 Dip N27] AS PL_V2SupplyVoltageDipsN27,
		[V2 Dip N28] AS PL_V2SupplyVoltageDipsN28,
		[V2 Dip N29] AS PL_V2SupplyVoltageDipsN29,
		[V2 Dip N30] AS PL_V2SupplyVoltageDipsN30,
		[V2 Dip N31] AS PL_V2SupplyVoltageDipsN31,
		[V2 Dip N32] AS PL_V2SupplyVoltageDipsN32,
		[V2 Dip N33] AS PL_V2SupplyVoltageDipsN33,
		[V2 Dip N34] AS PL_V2SupplyVoltageDipsN34,
		[V2 Dip N35] AS PL_V2SupplyVoltageDipsN35,
		[V2 Dip N36] AS PL_V2SupplyVoltageDipsN36,
		[V2 Dip N37] AS PL_V2SupplyVoltageDipsN37,
		[V2 Dip N38] AS PL_V2SupplyVoltageDipsN38,
		[V2 Dip N39] AS PL_V2SupplyVoltageDipsN39,
		[V2 Dip N40] AS PL_V2SupplyVoltageDipsN40,
		[V2 Dip N41] AS PL_V2SupplyVoltageDipsN41,
		[V2 Dip N42] AS PL_V2SupplyVoltageDipsN42,
		[V2 Dip N43] AS PL_V2SupplyVoltageDipsN43,
		[V2 Dip N44] AS PL_V2SupplyVoltageDipsN44,
		[V2 Dip N45] AS PL_V2SupplyVoltageDipsN45,
		[V2 Dip N46] AS PL_V2SupplyVoltageDipsN46,
		[V2 Dip N47] AS PL_V2SupplyVoltageDipsN47,
		[V2 Dip N48] AS PL_V2SupplyVoltageDipsN48,
		[V2 Dip N49] AS PL_V2SupplyVoltageDipsN49,
		[V2 Dip N50] AS PL_V2SupplyVoltageDipsN50,
		[V2 Dip N51] AS PL_V2SupplyVoltageDipsN51,
		[V2 Dip N52] AS PL_V2SupplyVoltageDipsN52,
		[V2 Dip N53] AS PL_V2SupplyVoltageDipsN53,
		[V2 Dip N54] AS PL_V2SupplyVoltageDipsN54,
		[V2 Dip N55] AS PL_V2SupplyVoltageDipsN55,
		[V2 Dip N56] AS PL_V2SupplyVoltageDipsN56,
		[V2 Dip N57] AS PL_V2SupplyVoltageDipsN57,
		[V2 Dip N58] AS PL_V2SupplyVoltageDipsN58,
		[V2 Dip N59] AS PL_V2SupplyVoltageDipsN59,
		[V2 Dip N60] AS PL_V2SupplyVoltageDipsN60,
		[V2 Dip N61] AS PL_V2SupplyVoltageDipsN61,
		[V2 Dip N62] AS PL_V2SupplyVoltageDipsN62,
		[V2 Dip N63] AS PL_V2SupplyVoltageDipsN63,
		[V2 Dip N64] AS PL_V2SupplyVoltageDipsN64,
		[V2 Dip N65] AS PL_V2SupplyVoltageDipsN65,
		[V2 Dip N66] AS PL_V2SupplyVoltageDipsN66,
		[V2 Dip N67] AS PL_V2SupplyVoltageDipsN67,
		[V2 Dip N68] AS PL_V2SupplyVoltageDipsN68,
		[V2 Dip N69] AS PL_V2SupplyVoltageDipsN69,
		[V2 Dip N70] AS PL_V2SupplyVoltageDipsN70,
		[V2 Dip N71] AS PL_V2SupplyVoltageDipsN71,
		[V2 Dip N72] AS PL_V2SupplyVoltageDipsN72,
		[V2 Dip N73] AS PL_V2SupplyVoltageDipsN73,
		[V2 Dip N74] AS PL_V2SupplyVoltageDipsN74,
		[V2 Dip N75] AS PL_V2SupplyVoltageDipsN75,
		[V2 Dip N76] AS PL_V2SupplyVoltageDipsN76,
		[V2 Dip N77] AS PL_V2SupplyVoltageDipsN77,
		[V2 Dip N78] AS PL_V2SupplyVoltageDipsN78,
		[V2 Dip N79] AS PL_V2SupplyVoltageDipsN79,
		[V2 Dip N80] AS PL_V2SupplyVoltageDipsN80,
		[V2 Dip N81] AS PL_V2SupplyVoltageDipsN81,
		[V2 Dip N82] AS PL_V2SupplyVoltageDipsN82,
		[V2 Dip N83] AS PL_V2SupplyVoltageDipsN83,
		[V2 Dip N84] AS PL_V2SupplyVoltageDipsN84,
		[V2 Dip N85] AS PL_V2SupplyVoltageDipsN85,
		[V2 Dip N86] AS PL_V2SupplyVoltageDipsN86,
		[V2 Dip N87] AS PL_V2SupplyVoltageDipsN87,
		[V2 Dip N88] AS PL_V2SupplyVoltageDipsN88,
		[V2 Dip N89] AS PL_V2SupplyVoltageDipsN89,
		[V2 Dip N90] AS PL_V2SupplyVoltageDipsN90,
		[V2 Dip N91] AS PL_V2SupplyVoltageDipsN91,
		[V2 Dip N92] AS PL_V2SupplyVoltageDipsN92,
		[V2 Dip N93] AS PL_V2SupplyVoltageDipsN93,
		[V2 Dip N94] AS PL_V2SupplyVoltageDipsN94,
		[V2 Dip N95] AS PL_V2SupplyVoltageDipsN95,
		[V2 Dip N96] AS PL_V2SupplyVoltageDipsN96,
		[V3 Dip N01] AS PL_V3SupplyVoltageDipsN01,
		[V3 Dip N02] AS PL_V3SupplyVoltageDipsN02,
		[V3 Dip N03] AS PL_V3SupplyVoltageDipsN03,
		[V3 Dip N04] AS PL_V3SupplyVoltageDipsN04,
		[V3 Dip N05] AS PL_V3SupplyVoltageDipsN05,
		[V3 Dip N06] AS PL_V3SupplyVoltageDipsN06,
		[V3 Dip N07] AS PL_V3SupplyVoltageDipsN07,
		[V3 Dip N08] AS PL_V3SupplyVoltageDipsN08,
		[V3 Dip N09] AS PL_V3SupplyVoltageDipsN09,
		[V3 Dip N10] AS PL_V3SupplyVoltageDipsN10,
		[V3 Dip N11] AS PL_V3SupplyVoltageDipsN11,
		[V3 Dip N12] AS PL_V3SupplyVoltageDipsN12,
		[V3 Dip N13] AS PL_V3SupplyVoltageDipsN13,
		[V3 Dip N14] AS PL_V3SupplyVoltageDipsN14,
		[V3 Dip N15] AS PL_V3SupplyVoltageDipsN15,
		[V3 Dip N16] AS PL_V3SupplyVoltageDipsN16,
		[V3 Dip N17] AS PL_V3SupplyVoltageDipsN17,
		[V3 Dip N18] AS PL_V3SupplyVoltageDipsN18,
		[V3 Dip N19] AS PL_V3SupplyVoltageDipsN19,
		[V3 Dip N20] AS PL_V3SupplyVoltageDipsN20,
		[V3 Dip N21] AS PL_V3SupplyVoltageDipsN21,
		[V3 Dip N22] AS PL_V3SupplyVoltageDipsN22,
		[V3 Dip N23] AS PL_V3SupplyVoltageDipsN23,
		[V3 Dip N24] AS PL_V3SupplyVoltageDipsN24,
		[V3 Dip N25] AS PL_V3SupplyVoltageDipsN25,
		[V3 Dip N26] AS PL_V3SupplyVoltageDipsN26,
		[V3 Dip N27] AS PL_V3SupplyVoltageDipsN27,
		[V3 Dip N28] AS PL_V3SupplyVoltageDipsN28,
		[V3 Dip N29] AS PL_V3SupplyVoltageDipsN29,
		[V3 Dip N30] AS PL_V3SupplyVoltageDipsN30,
		[V3 Dip N31] AS PL_V3SupplyVoltageDipsN31,
		[V3 Dip N32] AS PL_V3SupplyVoltageDipsN32,
		[V3 Dip N33] AS PL_V3SupplyVoltageDipsN33,
		[V3 Dip N34] AS PL_V3SupplyVoltageDipsN34,
		[V3 Dip N35] AS PL_V3SupplyVoltageDipsN35,
		[V3 Dip N36] AS PL_V3SupplyVoltageDipsN36,
		[V3 Dip N37] AS PL_V3SupplyVoltageDipsN37,
		[V3 Dip N38] AS PL_V3SupplyVoltageDipsN38,
		[V3 Dip N39] AS PL_V3SupplyVoltageDipsN39,
		[V3 Dip N40] AS PL_V3SupplyVoltageDipsN40,
		[V3 Dip N41] AS PL_V3SupplyVoltageDipsN41,
		[V3 Dip N42] AS PL_V3SupplyVoltageDipsN42,
		[V3 Dip N43] AS PL_V3SupplyVoltageDipsN43,
		[V3 Dip N44] AS PL_V3SupplyVoltageDipsN44,
		[V3 Dip N45] AS PL_V3SupplyVoltageDipsN45,
		[V3 Dip N46] AS PL_V3SupplyVoltageDipsN46,
		[V3 Dip N47] AS PL_V3SupplyVoltageDipsN47,
		[V3 Dip N48] AS PL_V3SupplyVoltageDipsN48,
		[V3 Dip N49] AS PL_V3SupplyVoltageDipsN49,
		[V3 Dip N50] AS PL_V3SupplyVoltageDipsN50,
		[V3 Dip N51] AS PL_V3SupplyVoltageDipsN51,
		[V3 Dip N52] AS PL_V3SupplyVoltageDipsN52,
		[V3 Dip N53] AS PL_V3SupplyVoltageDipsN53,
		[V3 Dip N54] AS PL_V3SupplyVoltageDipsN54,
		[V3 Dip N55] AS PL_V3SupplyVoltageDipsN55,
		[V3 Dip N56] AS PL_V3SupplyVoltageDipsN56,
		[V3 Dip N57] AS PL_V3SupplyVoltageDipsN57,
		[V3 Dip N58] AS PL_V3SupplyVoltageDipsN58,
		[V3 Dip N59] AS PL_V3SupplyVoltageDipsN59,
		[V3 Dip N60] AS PL_V3SupplyVoltageDipsN60,
		[V3 Dip N61] AS PL_V3SupplyVoltageDipsN61,
		[V3 Dip N62] AS PL_V3SupplyVoltageDipsN62,
		[V3 Dip N63] AS PL_V3SupplyVoltageDipsN63,
		[V3 Dip N64] AS PL_V3SupplyVoltageDipsN64,
		[V3 Dip N65] AS PL_V3SupplyVoltageDipsN65,
		[V3 Dip N66] AS PL_V3SupplyVoltageDipsN66,
		[V3 Dip N67] AS PL_V3SupplyVoltageDipsN67,
		[V3 Dip N68] AS PL_V3SupplyVoltageDipsN68,
		[V3 Dip N69] AS PL_V3SupplyVoltageDipsN69,
		[V3 Dip N70] AS PL_V3SupplyVoltageDipsN70,
		[V3 Dip N71] AS PL_V3SupplyVoltageDipsN71,
		[V3 Dip N72] AS PL_V3SupplyVoltageDipsN72,
		[V3 Dip N73] AS PL_V3SupplyVoltageDipsN73,
		[V3 Dip N74] AS PL_V3SupplyVoltageDipsN74,
		[V3 Dip N75] AS PL_V3SupplyVoltageDipsN75,
		[V3 Dip N76] AS PL_V3SupplyVoltageDipsN76,
		[V3 Dip N77] AS PL_V3SupplyVoltageDipsN77,
		[V3 Dip N78] AS PL_V3SupplyVoltageDipsN78,
		[V3 Dip N79] AS PL_V3SupplyVoltageDipsN79,
		[V3 Dip N80] AS PL_V3SupplyVoltageDipsN80,
		[V3 Dip N81] AS PL_V3SupplyVoltageDipsN81,
		[V3 Dip N82] AS PL_V3SupplyVoltageDipsN82,
		[V3 Dip N83] AS PL_V3SupplyVoltageDipsN83,
		[V3 Dip N84] AS PL_V3SupplyVoltageDipsN84,
		[V3 Dip N85] AS PL_V3SupplyVoltageDipsN85,
		[V3 Dip N86] AS PL_V3SupplyVoltageDipsN86,
		[V3 Dip N87] AS PL_V3SupplyVoltageDipsN87,
		[V3 Dip N88] AS PL_V3SupplyVoltageDipsN88,
		[V3 Dip N89] AS PL_V3SupplyVoltageDipsN89,
		[V3 Dip N90] AS PL_V3SupplyVoltageDipsN90,
		[V3 Dip N91] AS PL_V3SupplyVoltageDipsN91,
		[V3 Dip N92] AS PL_V3SupplyVoltageDipsN92,
		[V3 Dip N93] AS PL_V3SupplyVoltageDipsN93,
		[V3 Dip N94] AS PL_V3SupplyVoltageDipsN94,
		[V3 Dip N95] AS PL_V3SupplyVoltageDipsN95,
		[V3 Dip N96] AS PL_V3SupplyVoltageDipsN96
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[V1 Dip N01],
			[V1 Dip N02],
			[V1 Dip N03],
			[V1 Dip N04],
			[V1 Dip N05],
			[V1 Dip N06],
			[V1 Dip N07],
			[V1 Dip N08],
			[V1 Dip N09],
			[V1 Dip N10],
			[V1 Dip N11],
			[V1 Dip N12],
			[V1 Dip N13],
			[V1 Dip N14],
			[V1 Dip N15],
			[V1 Dip N16],
			[V1 Dip N17],
			[V1 Dip N18],
			[V1 Dip N19],
			[V1 Dip N20],
			[V1 Dip N21],
			[V1 Dip N22],
			[V1 Dip N23],
			[V1 Dip N24],
			[V1 Dip N25],
			[V1 Dip N26],
			[V1 Dip N27],
			[V1 Dip N28],
			[V1 Dip N29],
			[V1 Dip N30],
			[V1 Dip N31],
			[V1 Dip N32],
			[V1 Dip N33],
			[V1 Dip N34],
			[V1 Dip N35],
			[V1 Dip N36],
			[V1 Dip N37],
			[V1 Dip N38],
			[V1 Dip N39],
			[V1 Dip N40],
			[V1 Dip N41],
			[V1 Dip N42],
			[V1 Dip N43],
			[V1 Dip N44],
			[V1 Dip N45],
			[V1 Dip N46],
			[V1 Dip N47],
			[V1 Dip N48],
			[V1 Dip N49],
			[V1 Dip N50],
			[V1 Dip N51],
			[V1 Dip N52],
			[V1 Dip N53],
			[V1 Dip N54],
			[V1 Dip N55],
			[V1 Dip N56],
			[V1 Dip N57],
			[V1 Dip N58],
			[V1 Dip N59],
			[V1 Dip N60],
			[V1 Dip N61],
			[V1 Dip N62],
			[V1 Dip N63],
			[V1 Dip N64],
			[V1 Dip N65],
			[V1 Dip N66],
			[V1 Dip N67],
			[V1 Dip N68],
			[V1 Dip N69],
			[V1 Dip N70],
			[V1 Dip N71],
			[V1 Dip N72],
			[V1 Dip N73],
			[V1 Dip N74],
			[V1 Dip N75],
			[V1 Dip N76],
			[V1 Dip N77],
			[V1 Dip N78],
			[V1 Dip N79],
			[V1 Dip N80],
			[V1 Dip N81],
			[V1 Dip N82],
			[V1 Dip N83],
			[V1 Dip N84],
			[V1 Dip N85],
			[V1 Dip N86],
			[V1 Dip N87],
			[V1 Dip N88],
			[V1 Dip N89],
			[V1 Dip N90],
			[V1 Dip N91],
			[V1 Dip N92],
			[V1 Dip N93],
			[V1 Dip N94],
			[V1 Dip N95],
			[V1 Dip N96],
			[V2 Dip N01],
			[V2 Dip N02],
			[V2 Dip N03],
			[V2 Dip N04],
			[V2 Dip N05],
			[V2 Dip N06],
			[V2 Dip N07],
			[V2 Dip N08],
			[V2 Dip N09],
			[V2 Dip N10],
			[V2 Dip N11],
			[V2 Dip N12],
			[V2 Dip N13],
			[V2 Dip N14],
			[V2 Dip N15],
			[V2 Dip N16],
			[V2 Dip N17],
			[V2 Dip N18],
			[V2 Dip N19],
			[V2 Dip N20],
			[V2 Dip N21],
			[V2 Dip N22],
			[V2 Dip N23],
			[V2 Dip N24],
			[V2 Dip N25],
			[V2 Dip N26],
			[V2 Dip N27],
			[V2 Dip N28],
			[V2 Dip N29],
			[V2 Dip N30],
			[V2 Dip N31],
			[V2 Dip N32],
			[V2 Dip N33],
			[V2 Dip N34],
			[V2 Dip N35],
			[V2 Dip N36],
			[V2 Dip N37],
			[V2 Dip N38],
			[V2 Dip N39],
			[V2 Dip N40],
			[V2 Dip N41],
			[V2 Dip N42],
			[V2 Dip N43],
			[V2 Dip N44],
			[V2 Dip N45],
			[V2 Dip N46],
			[V2 Dip N47],
			[V2 Dip N48],
			[V2 Dip N49],
			[V2 Dip N50],
			[V2 Dip N51],
			[V2 Dip N52],
			[V2 Dip N53],
			[V2 Dip N54],
			[V2 Dip N55],
			[V2 Dip N56],
			[V2 Dip N57],
			[V2 Dip N58],
			[V2 Dip N59],
			[V2 Dip N60],
			[V2 Dip N61],
			[V2 Dip N62],
			[V2 Dip N63],
			[V2 Dip N64],
			[V2 Dip N65],
			[V2 Dip N66],
			[V2 Dip N67],
			[V2 Dip N68],
			[V2 Dip N69],
			[V2 Dip N70],
			[V2 Dip N71],
			[V2 Dip N72],
			[V2 Dip N73],
			[V2 Dip N74],
			[V2 Dip N75],
			[V2 Dip N76],
			[V2 Dip N77],
			[V2 Dip N78],
			[V2 Dip N79],
			[V2 Dip N80],
			[V2 Dip N81],
			[V2 Dip N82],
			[V2 Dip N83],
			[V2 Dip N84],
			[V2 Dip N85],
			[V2 Dip N86],
			[V2 Dip N87],
			[V2 Dip N88],
			[V2 Dip N89],
			[V2 Dip N90],
			[V2 Dip N91],
			[V2 Dip N92],
			[V2 Dip N93],
			[V2 Dip N94],
			[V2 Dip N95],
			[V2 Dip N96],
			[V3 Dip N01],
			[V3 Dip N02],
			[V3 Dip N03],
			[V3 Dip N04],
			[V3 Dip N05],
			[V3 Dip N06],
			[V3 Dip N07],
			[V3 Dip N08],
			[V3 Dip N09],
			[V3 Dip N10],
			[V3 Dip N11],
			[V3 Dip N12],
			[V3 Dip N13],
			[V3 Dip N14],
			[V3 Dip N15],
			[V3 Dip N16],
			[V3 Dip N17],
			[V3 Dip N18],
			[V3 Dip N19],
			[V3 Dip N20],
			[V3 Dip N21],
			[V3 Dip N22],
			[V3 Dip N23],
			[V3 Dip N24],
			[V3 Dip N25],
			[V3 Dip N26],
			[V3 Dip N27],
			[V3 Dip N28],
			[V3 Dip N29],
			[V3 Dip N30],
			[V3 Dip N31],
			[V3 Dip N32],
			[V3 Dip N33],
			[V3 Dip N34],
			[V3 Dip N35],
			[V3 Dip N36],
			[V3 Dip N37],
			[V3 Dip N38],
			[V3 Dip N39],
			[V3 Dip N40],
			[V3 Dip N41],
			[V3 Dip N42],
			[V3 Dip N43],
			[V3 Dip N44],
			[V3 Dip N45],
			[V3 Dip N46],
			[V3 Dip N47],
			[V3 Dip N48],
			[V3 Dip N49],
			[V3 Dip N50],
			[V3 Dip N51],
			[V3 Dip N52],
			[V3 Dip N53],
			[V3 Dip N54],
			[V3 Dip N55],
			[V3 Dip N56],
			[V3 Dip N57],
			[V3 Dip N58],
			[V3 Dip N59],
			[V3 Dip N60],
			[V3 Dip N61],
			[V3 Dip N62],
			[V3 Dip N63],
			[V3 Dip N64],
			[V3 Dip N65],
			[V3 Dip N66],
			[V3 Dip N67],
			[V3 Dip N68],
			[V3 Dip N69],
			[V3 Dip N70],
			[V3 Dip N71],
			[V3 Dip N72],
			[V3 Dip N73],
			[V3 Dip N74],
			[V3 Dip N75],
			[V3 Dip N76],
			[V3 Dip N77],
			[V3 Dip N78],
			[V3 Dip N79],
			[V3 Dip N80],
			[V3 Dip N81],
			[V3 Dip N82],
			[V3 Dip N83],
			[V3 Dip N84],
			[V3 Dip N85],
			[V3 Dip N86],
			[V3 Dip N87],
			[V3 Dip N88],
			[V3 Dip N89],
			[V3 Dip N90],
			[V3 Dip N91],
			[V3 Dip N92],
			[V3 Dip N93],
			[V3 Dip N94],
			[V3 Dip N95],
			[V3 Dip N96]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID
GO

/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Supply Voltage Magnitude data for the devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160SupplyVoltageMagnitudeDataION.sql#2 $
******************************************************************************/
ALTER PROCEDURE [dbo].[spDAL_GetEN50160SupplyVoltageMagnitudeDataION]
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS
-- find information about the sources we have selected
DECLARE @TblSources TABLE
(
	ID							INT PRIMARY KEY NOT NULL,
	MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
)

INSERT INTO
	@TblSources
SELECT
	ID,
	UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
FROM
	dbo.udfDAL_GetSources(@SourceIDs)

-- define the character that we use to separate the different dates
-- within the lists of start and end dates
DECLARE @DelimiterCharacter NCHAR(1)
SET @DelimiterCharacter = CHAR(6)

-- create a table variable to hold the EN50160 results
DECLARE @EN50160_SupplyVoltageMagnitude TABLE
(
	TimestampUtc						DATETIME,
	SourceID							INT,
    V1SupplyVoltageMagnitudeN			FLOAT,
    V1SupplyVoltageMagnitudeN1			FLOAT,
    V1SupplyVoltageMagnitudeN1Eval		FLOAT,
	V1SupplyVoltageMagnitudeN2Eval		FLOAT,
    V1SupplyVoltageMagnitudeNInvalid	FLOAT,
	V2SupplyVoltageMagnitudeN			FLOAT,
	V2SupplyVoltageMagnitudeN1			FLOAT,
    V2SupplyVoltageMagnitudeN1Eval		FLOAT,
	V2SupplyVoltageMagnitudeN2Eval		FLOAT,
    V2SupplyVoltageMagnitudeNInvalid	FLOAT,
    V3SupplyVoltageMagnitudeN			FLOAT,
    V3SupplyVoltageMagnitudeN1			FLOAT,
    V3SupplyVoltageMagnitudeN1Eval		FLOAT,
	V3SupplyVoltageMagnitudeN2Eval		FLOAT,
    V3SupplyVoltageMagnitudeNInvalid	FLOAT
)
 
-- put the raw data in a form that's easier to query
DECLARE @TblData TABLE
(
	SourceID		INT,
	MeasurementName	NVARCHAR(200),
	TimestampUtc	DATETIME,
	Value			FLOAT
)

-- insert data for the ION devices into the staging table
INSERT INTO
	@TblData
SELECT
	s.ID AS SourceID,
	q.Name AS MeasurementName,
	dl.TimestampUTC AS TimestampUtc,
	dl.Value
FROM
	dbo.DataLog2 dl
	INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
	INNER JOIN @TblSources s on dl.SourceID = s.ID
WHERE
	s.MeasurementSetIdentifier = N'ION'
	AND q.Name IN
	(
		N'Voltage Magnitude Count N on Input V1',
		N'Voltage Magnitude Count N1 on Input V1',
		N'Voltage Magnitude Count N invalid on Input V1',
		N'Voltage Magnitude Count N on Input V2',
		N'Voltage Magnitude Count N1 on Input V2',
		N'Voltage Magnitude Count N invalid on Input V2',
		N'Voltage Magnitude Count N on Input V3',
		N'Voltage Magnitude Count N1 on Input V3',
		N'Voltage Magnitude Count N invalid on Input V3'
	)
	AND EXISTS
	(
		SELECT
			1
		FROM
			dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
		WHERE
			dl.TimestampUTC > ts.StartDate 
			AND dl.TimestampUTC <= ts.EndDate
	)
ORDER BY
	TimestampUTC,
	SourceID,
	q.Name

-- select data for the ION devices into the results table
SELECT
	TimestampUtc,
	SourceID,
    [Voltage Magnitude Count N on Input V1]			AS V1SupplyVoltageMagnitudeN,
    [Voltage Magnitude Count N1 on Input V1]		AS V1SupplyVoltageMagnitudeN1,
	CASE [Voltage Magnitude Count N on Input V1]
		WHEN 0 THEN NULL
		ELSE [Voltage Magnitude Count N1 on Input V1]/[Voltage Magnitude Count N on Input V1]
	END AS V1SupplyVoltageMagnitudeN1Eval,
	NULL											AS V1SupplyVoltageMagnitudeN2Eval,
    [Voltage Magnitude Count N invalid on Input V1]	AS V1SupplyVoltageMagnitudeNInvalid,
	[Voltage Magnitude Count N on Input V2]			AS V2SupplyVoltageMagnitudeN,
	[Voltage Magnitude Count N1 on Input V2]		AS V2SupplyVoltageMagnitudeN1,
	CASE [Voltage Magnitude Count N on Input V2]
		WHEN 0 THEN NULL
		ELSE [Voltage Magnitude Count N1 on Input V2]/[Voltage Magnitude Count N on Input V2]
	END AS V2SupplyVoltageMagnitudeN1Eval,
	NULL											AS V2SupplyVoltageMagnitudeN2Eval,
    [Voltage Magnitude Count N invalid on Input V2]	AS V2SupplyVoltageMagnitudeNInvalid,
    [Voltage Magnitude Count N on Input V3]			AS V3SupplyVoltageMagnitudeN,
    [Voltage Magnitude Count N1 on Input V3]		AS V3SupplyVoltageMagnitudeN1,
	CASE [Voltage Magnitude Count N on Input V3]
		WHEN 0 THEN NULL
		ELSE [Voltage Magnitude Count N1 on Input V3]/[Voltage Magnitude Count N on Input V3]
	END AS V3SupplyVoltageMagnitudeN1Eval,
	NULL											AS V3SupplyVoltageMagnitudeN2Eval,
    [Voltage Magnitude Count N invalid on Input V3]	AS V3SupplyVoltageMagnitudeNInvalid
FROM
	@TblData
PIVOT
(
	MAX(Value)
	FOR MeasurementName IN
	(
		[Voltage Magnitude Count N on Input V1],
		[Voltage Magnitude Count N1 on Input V1],
		[Voltage Magnitude Count N invalid on Input V1],
		[Voltage Magnitude Count N on Input V2],
		[Voltage Magnitude Count N1 on Input V2],
		[Voltage Magnitude Count N invalid on Input V2],
		[Voltage Magnitude Count N on Input V3],
		[Voltage Magnitude Count N1 on Input V3],
		[Voltage Magnitude Count N invalid on Input V3]
	)
) AS pvt
ORDER BY
	TimestampUtc,
	SourceID

GO


/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Supply Voltage Magnitude data for the devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160SupplyVoltageMagnitudeDataPL.sql#2 $
******************************************************************************/
ALTER PROCEDURE [dbo].[spDAL_GetEN50160SupplyVoltageMagnitudeDataPL]
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS
-- find information about the sources we have selected
DECLARE @TblSources TABLE
(
	ID							INT PRIMARY KEY NOT NULL,	
	MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
)

INSERT INTO
	@TblSources
SELECT
	ID,	
	UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
FROM
	dbo.udfDAL_GetSources(@SourceIDs)

-- define the character that we use to separate the different dates
-- within the lists of start and end dates
DECLARE @DelimiterCharacter NCHAR(1)
SET @DelimiterCharacter = CHAR(6)

-- create a table variable to hold the EN50160 results
DECLARE @EN50160_SupplyVoltageMagnitude TABLE
(
	TimestampUtc						DATETIME,
	SourceID							INT,
    V1SupplyVoltageMagnitudeN1Eval		FLOAT,
	V1SupplyVoltageMagnitudeN2Eval		FLOAT,
    V2SupplyVoltageMagnitudeN1Eval		FLOAT,
	V2SupplyVoltageMagnitudeN2Eval		FLOAT,
    V3SupplyVoltageMagnitudeN1Eval		FLOAT,
	V3SupplyVoltageMagnitudeN2Eval		FLOAT
)
 
-- put the raw data in a form that's easier to query
DECLARE @TblData TABLE
(
	SourceID		INT,
	MeasurementName	NVARCHAR(200),
	TimestampUtc	DATETIME,
	Value			FLOAT
)

INSERT INTO
	@TblData
SELECT
	s.ID AS SourceID,
	q.Name AS MeasurementName,
	dl.TimestampUTC AS TimestampUtc,
	dl.Value
FROM
	dbo.DataLog2 dl
	INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
	INNER JOIN @TblSources s on dl.SourceID = s.ID
WHERE
	s.MeasurementSetIdentifier = N'PL'
	AND q.Name IN
	(
		N'V1 Mag N1 Eval',
		N'V1 Mag N2 Eval',
		N'V2 Mag N1 Eval',
		N'V2 Mag N2 Eval',
		N'V3 Mag N1 Eval',
		N'V3 Mag N2 Eval'
	)
	AND EXISTS
	(
		SELECT
			1
		FROM
			dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
		WHERE
			dl.TimestampUTC > ts.StartDate 
			AND dl.TimestampUTC <= ts.EndDate
	)
ORDER BY
	TimestampUTC,
	SourceID,
	q.Name

-- select data for the PowerLogic (PL) devices into the results table
SELECT
	TimestampUtc,
	SourceID,
	[V1 Mag N1 Eval]	AS V1SupplyVoltageMagnitudeN1Eval,
	[V1 Mag N2 Eval]	AS V1SupplyVoltageMagnitudeN2Eval,
	[V2 Mag N1 Eval]	AS V2SupplyVoltageMagnitudeN1Eval,
	[V2 Mag N2 Eval]	AS V2SupplyVoltageMagnitudeN2Eval,
	[V3 Mag N1 Eval]	AS V3SupplyVoltageMagnitudeN1Eval,
	[V3 Mag N2 Eval]	AS V3SupplyVoltageMagnitudeN2Eval
FROM
	@TblData
PIVOT
(
	MAX(Value)
	FOR MeasurementName IN
	(
		[V1 Mag N1 Eval],
		[V1 Mag N2 Eval],
		[V2 Mag N1 Eval],
		[V2 Mag N2 Eval],
		[V3 Mag N1 Eval],
		[V3 Mag N2 Eval]
	)
) AS pvt
ORDER BY
	TimestampUtc,
	SourceID

GO


/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Supply Voltage Unbalance data for the devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160SupplyVoltageUnbalanceDataION.sql#2 $
******************************************************************************/
ALTER PROCEDURE [dbo].[spDAL_GetEN50160SupplyVoltageUnbalanceDataION]
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID							INT PRIMARY KEY NOT NULL,
		MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_SupplyVoltageUnbalance TABLE
	(
		TimestampUtc					DATETIME,
		SourceID						INT,
        SupplyVoltageUnbalanceN			FLOAT,
        SupplyVoltageUnbalanceN1		FLOAT,
        SupplyVoltageUnbalanceN1Eval	FLOAT,
        SupplyVoltageUnbalanceNInvalid	FLOAT
	)
	 
	-- put the raw data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME,
		Value			FLOAT
	)
	-- insert data for the ION devices into the staging table
	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		s.MeasurementSetIdentifier = N'ION'
		AND q.Name IN
		(
			N'Voltage Unbalance Count N',
			N'Voltage Unbalance Count N1',
			N'Voltage Unbalance Count N invalid'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name

	-- select data for the ION devices into the results table
	SELECT
		TimestampUtc,
		SourceID,
        [Voltage Unbalance Count N]			AS SupplyVoltageUnbalanceN,
        [Voltage Unbalance Count N1]		AS SupplyVoltageUnbalanceN1,
        CASE [Voltage Unbalance Count N]
			WHEN 0 THEN NULL
			ELSE [Voltage Unbalance Count N1]/[Voltage Unbalance Count N]
		END AS SupplyVoltageUnbalanceN1Eval,
        [Voltage Unbalance Count N invalid]	AS SupplyVoltageUnbalanceNInvalid
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[Voltage Unbalance Count N],
			[Voltage Unbalance Count N1],
			[Voltage Unbalance Count N invalid]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID

GO


/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Supply Voltage Unbalance data for the devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160SupplyVoltageUnbalanceDataPL.sql#2 $
******************************************************************************/
ALTER PROCEDURE [dbo].[spDAL_GetEN50160SupplyVoltageUnbalanceDataPL]
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID							INT PRIMARY KEY NOT NULL,
		MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_SupplyVoltageUnbalance TABLE
	(
		TimestampUtc					DATETIME,
		SourceID						INT,
        SupplyVoltageUnbalanceN1Eval	FLOAT        
	)
	 
	-- put the raw data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME,
		Value			FLOAT
	)

	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		s.MeasurementSetIdentifier = N'PL'
		AND q.Name IN
		(
			N'Unbalance N1 Eval'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name

	-- select data for the PowerLogic (PL) devices into the results table
	SELECT
		TimestampUtc,
		SourceID,
        [Unbalance N1 Eval]	AS SupplyVoltageUnbalanceN1Eval        
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[Unbalance N1 Eval]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID

GO


/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Temporary Overvoltages data for the ION devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160TemporaryOvervoltagesDataION.sql#2 $
******************************************************************************/
ALTER PROCEDURE dbo.spDAL_GetEN50160TemporaryOvervoltagesDataION
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX),
	@OutputDataOnly	BIT = 0
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID							INT PRIMARY KEY NOT NULL,
		MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_TemporaryOvervoltages TABLE
	(
		TimestampUtc			DATETIME,
		SourceID				INT,
		Ion_V1OvervoltagesN11	FLOAT,
		Ion_V1OvervoltagesN12	FLOAT,
		Ion_V1OvervoltagesN13	FLOAT,
		Ion_V1OvervoltagesN14	FLOAT,
		Ion_V1OvervoltagesN15	FLOAT,
		Ion_V1OvervoltagesN21	FLOAT,
		Ion_V1OvervoltagesN22	FLOAT,
		Ion_V1OvervoltagesN23	FLOAT,
		Ion_V1OvervoltagesN24	FLOAT,
		Ion_V1OvervoltagesN25	FLOAT,
		Ion_V1OvervoltagesN31	FLOAT,
		Ion_V1OvervoltagesN32	FLOAT,
		Ion_V1OvervoltagesN33	FLOAT,
		Ion_V1OvervoltagesN34	FLOAT,
		Ion_V1OvervoltagesN35	FLOAT,
		Ion_V2OvervoltagesN11	FLOAT,
		Ion_V2OvervoltagesN12	FLOAT,
		Ion_V2OvervoltagesN13	FLOAT,
		Ion_V2OvervoltagesN14	FLOAT,
		Ion_V2OvervoltagesN15	FLOAT,
		Ion_V2OvervoltagesN21	FLOAT,
		Ion_V2OvervoltagesN22	FLOAT,
		Ion_V2OvervoltagesN23	FLOAT,
		Ion_V2OvervoltagesN24	FLOAT,
		Ion_V2OvervoltagesN25	FLOAT,
		Ion_V2OvervoltagesN31	FLOAT,
		Ion_V2OvervoltagesN32	FLOAT,
		Ion_V2OvervoltagesN33	FLOAT,
		Ion_V2OvervoltagesN34	FLOAT,
		Ion_V2OvervoltagesN35	FLOAT,
		Ion_V3OvervoltagesN11	FLOAT,
		Ion_V3OvervoltagesN12	FLOAT,
		Ion_V3OvervoltagesN13	FLOAT,
		Ion_V3OvervoltagesN14	FLOAT,
		Ion_V3OvervoltagesN15	FLOAT,
		Ion_V3OvervoltagesN21	FLOAT,
		Ion_V3OvervoltagesN22	FLOAT,
		Ion_V3OvervoltagesN23	FLOAT,
		Ion_V3OvervoltagesN24	FLOAT,
		Ion_V3OvervoltagesN25	FLOAT,
		Ion_V3OvervoltagesN31	FLOAT,
		Ion_V3OvervoltagesN32	FLOAT,
		Ion_V3OvervoltagesN33	FLOAT,
		Ion_V3OvervoltagesN34	FLOAT,
		Ion_V3OvervoltagesN35	FLOAT
	)
	 
	-- put the raw data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME,
		Value			FLOAT
	)

	-- insert data for the ION devices into the staging table
	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		s.MeasurementSetIdentifier = N'ION'
		AND q.Name IN
		(
			N'Overvoltage Count N11 on Input V1',
			N'Overvoltage Count N12 on Input V1',
			N'Overvoltage Count N13 on Input V1',
			N'Overvoltage Count N14 on Input V1',
			N'Overvoltage Count N15 on Input V1',
			N'Overvoltage Count N21 on Input V1',
			N'Overvoltage Count N22 on Input V1',
			N'Overvoltage Count N23 on Input V1',
			N'Overvoltage Count N24 on Input V1',
			N'Overvoltage Count N25 on Input V1',
			N'Overvoltage Count N31 on Input V1',
			N'Overvoltage Count N32 on Input V1',
			N'Overvoltage Count N33 on Input V1',
			N'Overvoltage Count N34 on Input V1',
			N'Overvoltage Count N35 on Input V1',
			N'Overvoltage Count N11 on Input V2',
			N'Overvoltage Count N12 on Input V2',
			N'Overvoltage Count N13 on Input V2',
			N'Overvoltage Count N14 on Input V2',
			N'Overvoltage Count N15 on Input V2',
			N'Overvoltage Count N21 on Input V2',
			N'Overvoltage Count N22 on Input V2',
			N'Overvoltage Count N23 on Input V2',
			N'Overvoltage Count N24 on Input V2',
			N'Overvoltage Count N25 on Input V2',
			N'Overvoltage Count N31 on Input V2',
			N'Overvoltage Count N32 on Input V2',
			N'Overvoltage Count N33 on Input V2',
			N'Overvoltage Count N34 on Input V2',
			N'Overvoltage Count N35 on Input V2',
			N'Overvoltage Count N11 on Input V3',
			N'Overvoltage Count N12 on Input V3',
			N'Overvoltage Count N13 on Input V3',
			N'Overvoltage Count N14 on Input V3',
			N'Overvoltage Count N15 on Input V3',
			N'Overvoltage Count N21 on Input V3',
			N'Overvoltage Count N22 on Input V3',
			N'Overvoltage Count N23 on Input V3',
			N'Overvoltage Count N24 on Input V3',
			N'Overvoltage Count N25 on Input V3',
			N'Overvoltage Count N31 on Input V3',
			N'Overvoltage Count N32 on Input V3',
			N'Overvoltage Count N33 on Input V3',
			N'Overvoltage Count N34 on Input V3',
			N'Overvoltage Count N35 on Input V3'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name

	-- select data for the ION devices into the results table
	SELECT
		TimestampUtc,
		SourceID,
		[Overvoltage Count N11 on Input V1] AS Ion_V1OvervoltagesN11,
		[Overvoltage Count N12 on Input V1] AS Ion_V1OvervoltagesN12,
		[Overvoltage Count N13 on Input V1] AS Ion_V1OvervoltagesN13,
		[Overvoltage Count N14 on Input V1] AS Ion_V1OvervoltagesN14,
		[Overvoltage Count N15 on Input V1] AS Ion_V1OvervoltagesN15,
		[Overvoltage Count N21 on Input V1] AS Ion_V1OvervoltagesN21,
		[Overvoltage Count N22 on Input V1] AS Ion_V1OvervoltagesN22,
		[Overvoltage Count N23 on Input V1] AS Ion_V1OvervoltagesN23,
		[Overvoltage Count N24 on Input V1] AS Ion_V1OvervoltagesN24,
		[Overvoltage Count N25 on Input V1] AS Ion_V1OvervoltagesN25,
		[Overvoltage Count N31 on Input V1] AS Ion_V1OvervoltagesN31,
		[Overvoltage Count N32 on Input V1] AS Ion_V1OvervoltagesN32,
		[Overvoltage Count N33 on Input V1] AS Ion_V1OvervoltagesN33,
		[Overvoltage Count N34 on Input V1] AS Ion_V1OvervoltagesN34,
		[Overvoltage Count N35 on Input V1] AS Ion_V1OvervoltagesN35,
		[Overvoltage Count N11 on Input V2] AS Ion_V2OvervoltagesN11,
		[Overvoltage Count N12 on Input V2] AS Ion_V2OvervoltagesN12,
		[Overvoltage Count N13 on Input V2] AS Ion_V2OvervoltagesN13,
		[Overvoltage Count N14 on Input V2] AS Ion_V2OvervoltagesN14,
		[Overvoltage Count N15 on Input V2] AS Ion_V2OvervoltagesN15,
		[Overvoltage Count N21 on Input V2] AS Ion_V2OvervoltagesN21,
		[Overvoltage Count N22 on Input V2] AS Ion_V2OvervoltagesN22,
		[Overvoltage Count N23 on Input V2] AS Ion_V2OvervoltagesN23,
		[Overvoltage Count N24 on Input V2] AS Ion_V2OvervoltagesN24,
		[Overvoltage Count N25 on Input V2] AS Ion_V2OvervoltagesN25,
		[Overvoltage Count N31 on Input V2] AS Ion_V2OvervoltagesN31,
		[Overvoltage Count N32 on Input V2] AS Ion_V2OvervoltagesN32,
		[Overvoltage Count N33 on Input V2] AS Ion_V2OvervoltagesN33,
		[Overvoltage Count N34 on Input V2] AS Ion_V2OvervoltagesN34,
		[Overvoltage Count N35 on Input V2] AS Ion_V2OvervoltagesN35,
		[Overvoltage Count N11 on Input V3] AS Ion_V3OvervoltagesN11,
		[Overvoltage Count N12 on Input V3] AS Ion_V3OvervoltagesN12,
		[Overvoltage Count N13 on Input V3] AS Ion_V3OvervoltagesN13,
		[Overvoltage Count N14 on Input V3] AS Ion_V3OvervoltagesN14,
		[Overvoltage Count N15 on Input V3] AS Ion_V3OvervoltagesN15,
		[Overvoltage Count N21 on Input V3] AS Ion_V3OvervoltagesN21,
		[Overvoltage Count N22 on Input V3] AS Ion_V3OvervoltagesN22,
		[Overvoltage Count N23 on Input V3] AS Ion_V3OvervoltagesN23,
		[Overvoltage Count N24 on Input V3] AS Ion_V3OvervoltagesN24,
		[Overvoltage Count N25 on Input V3] AS Ion_V3OvervoltagesN25,
		[Overvoltage Count N31 on Input V3] AS Ion_V3OvervoltagesN31,
		[Overvoltage Count N32 on Input V3] AS Ion_V3OvervoltagesN32,
		[Overvoltage Count N33 on Input V3] AS Ion_V3OvervoltagesN33,
		[Overvoltage Count N34 on Input V3] AS Ion_V3OvervoltagesN34,
		[Overvoltage Count N35 on Input V3] AS Ion_V3OvervoltagesN35
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[Overvoltage Count N11 on Input V1],
			[Overvoltage Count N12 on Input V1],
			[Overvoltage Count N13 on Input V1],
			[Overvoltage Count N14 on Input V1],
			[Overvoltage Count N15 on Input V1],
			[Overvoltage Count N21 on Input V1],
			[Overvoltage Count N22 on Input V1],
			[Overvoltage Count N23 on Input V1],
			[Overvoltage Count N24 on Input V1],
			[Overvoltage Count N25 on Input V1],
			[Overvoltage Count N31 on Input V1],
			[Overvoltage Count N32 on Input V1],
			[Overvoltage Count N33 on Input V1],
			[Overvoltage Count N34 on Input V1],
			[Overvoltage Count N35 on Input V1],
			[Overvoltage Count N11 on Input V2],
			[Overvoltage Count N12 on Input V2],
			[Overvoltage Count N13 on Input V2],
			[Overvoltage Count N14 on Input V2],
			[Overvoltage Count N15 on Input V2],
			[Overvoltage Count N21 on Input V2],
			[Overvoltage Count N22 on Input V2],
			[Overvoltage Count N23 on Input V2],
			[Overvoltage Count N24 on Input V2],
			[Overvoltage Count N25 on Input V2],
			[Overvoltage Count N31 on Input V2],
			[Overvoltage Count N32 on Input V2],
			[Overvoltage Count N33 on Input V2],
			[Overvoltage Count N34 on Input V2],
			[Overvoltage Count N35 on Input V2],
			[Overvoltage Count N11 on Input V3],
			[Overvoltage Count N12 on Input V3],
			[Overvoltage Count N13 on Input V3],
			[Overvoltage Count N14 on Input V3],
			[Overvoltage Count N15 on Input V3],
			[Overvoltage Count N21 on Input V3],
			[Overvoltage Count N22 on Input V3],
			[Overvoltage Count N23 on Input V3],
			[Overvoltage Count N24 on Input V3],
			[Overvoltage Count N25 on Input V3],
			[Overvoltage Count N31 on Input V3],
			[Overvoltage Count N32 on Input V3],
			[Overvoltage Count N33 on Input V3],
			[Overvoltage Count N34 on Input V3],
			[Overvoltage Count N35 on Input V3]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID
GO


/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Temporary Overvoltages data for the PowerLogic devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160TemporaryOvervoltagesDataPL.sql#2 $
******************************************************************************/
ALTER PROCEDURE dbo.spDAL_GetEN50160TemporaryOvervoltagesDataPL
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID							INT PRIMARY KEY NOT NULL,
		MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- create a table variable to hold the EN50160 results
	DECLARE @EN50160_TemporaryOvervoltages TABLE
	(
		TimestampUtc			DATETIME,
		SourceID				INT,
		PL_V1OvervoltagesN01	FLOAT,
		PL_V1OvervoltagesN02	FLOAT,
		PL_V1OvervoltagesN03	FLOAT,
		PL_V1OvervoltagesN04	FLOAT,
		PL_V1OvervoltagesN05	FLOAT,
		PL_V1OvervoltagesN06	FLOAT,
		PL_V1OvervoltagesN07	FLOAT,
		PL_V1OvervoltagesN08	FLOAT,
		PL_V1OvervoltagesN09	FLOAT,
		PL_V1OvervoltagesN10	FLOAT,
		PL_V1OvervoltagesN11	FLOAT,
		PL_V1OvervoltagesN12	FLOAT,
		PL_V1OvervoltagesN13	FLOAT,
		PL_V1OvervoltagesN14	FLOAT,
		PL_V1OvervoltagesN15	FLOAT,
		PL_V1OvervoltagesN16	FLOAT,
		PL_V1OvervoltagesN17	FLOAT,
		PL_V1OvervoltagesN18	FLOAT,
		PL_V1OvervoltagesN19	FLOAT,
		PL_V1OvervoltagesN20	FLOAT,
		PL_V1OvervoltagesN21	FLOAT,
		PL_V1OvervoltagesN22	FLOAT,
		PL_V1OvervoltagesN23	FLOAT,
		PL_V1OvervoltagesN24	FLOAT,
		PL_V1OvervoltagesN25	FLOAT,
		PL_V1OvervoltagesN26	FLOAT,
		PL_V1OvervoltagesN27	FLOAT,
		PL_V1OvervoltagesN28	FLOAT,
		PL_V1OvervoltagesN29	FLOAT,
		PL_V1OvervoltagesN30	FLOAT,
		PL_V1OvervoltagesN31	FLOAT,
		PL_V1OvervoltagesN32	FLOAT,
		PL_V1OvervoltagesN33	FLOAT,
		PL_V1OvervoltagesN34	FLOAT,
		PL_V1OvervoltagesN35	FLOAT,
		PL_V1OvervoltagesN36	FLOAT,
		PL_V1OvervoltagesN37	FLOAT,
		PL_V1OvervoltagesN38	FLOAT,
		PL_V1OvervoltagesN39	FLOAT,
		PL_V1OvervoltagesN40	FLOAT,
		PL_V1OvervoltagesN41	FLOAT,
		PL_V1OvervoltagesN42	FLOAT,
		PL_V1OvervoltagesN43	FLOAT,
		PL_V1OvervoltagesN44	FLOAT,
		PL_V1OvervoltagesN45	FLOAT,
		PL_V1OvervoltagesN46	FLOAT,
		PL_V1OvervoltagesN47	FLOAT,
		PL_V1OvervoltagesN48	FLOAT,
		PL_V1OvervoltagesN49	FLOAT,
		PL_V1OvervoltagesN50	FLOAT,
		PL_V1OvervoltagesN51	FLOAT,
		PL_V1OvervoltagesN52	FLOAT,
		PL_V1OvervoltagesN53	FLOAT,
		PL_V1OvervoltagesN54	FLOAT,
		PL_V1OvervoltagesN55	FLOAT,
		PL_V1OvervoltagesN56	FLOAT,
		PL_V1OvervoltagesN57	FLOAT,
		PL_V1OvervoltagesN58	FLOAT,
		PL_V1OvervoltagesN59	FLOAT,
		PL_V1OvervoltagesN60	FLOAT,
		PL_V1OvervoltagesN61	FLOAT,
		PL_V1OvervoltagesN62	FLOAT,
		PL_V1OvervoltagesN63	FLOAT,
		PL_V1OvervoltagesN64	FLOAT,
		PL_V1OvervoltagesN65	FLOAT,
		PL_V1OvervoltagesN66	FLOAT,
		PL_V1OvervoltagesN67	FLOAT,
		PL_V1OvervoltagesN68	FLOAT,
		PL_V1OvervoltagesN69	FLOAT,
		PL_V1OvervoltagesN70	FLOAT,
		PL_V1OvervoltagesN71	FLOAT,
		PL_V1OvervoltagesN72	FLOAT,
		PL_V1OvervoltagesN73	FLOAT,
		PL_V1OvervoltagesN74	FLOAT,
		PL_V1OvervoltagesN75	FLOAT,
		PL_V1OvervoltagesN76	FLOAT,
		PL_V1OvervoltagesN77	FLOAT,
		PL_V1OvervoltagesN78	FLOAT,
		PL_V1OvervoltagesN79	FLOAT,
		PL_V1OvervoltagesN80	FLOAT,
		PL_V1OvervoltagesN81	FLOAT,
		PL_V1OvervoltagesN82	FLOAT,
		PL_V1OvervoltagesN83	FLOAT,
		PL_V1OvervoltagesN84	FLOAT,
		PL_V1OvervoltagesN85	FLOAT,
		PL_V1OvervoltagesN86	FLOAT,
		PL_V1OvervoltagesN87	FLOAT,
		PL_V1OvervoltagesN88	FLOAT,
		PL_V1OvervoltagesN89	FLOAT,
		PL_V1OvervoltagesN90	FLOAT,
		PL_V1OvervoltagesN91	FLOAT,
		PL_V1OvervoltagesN92	FLOAT,
		PL_V1OvervoltagesN93	FLOAT,
		PL_V1OvervoltagesN94	FLOAT,
		PL_V1OvervoltagesN95	FLOAT,
		PL_V1OvervoltagesN96	FLOAT,
		PL_V2OvervoltagesN01	FLOAT,
		PL_V2OvervoltagesN02	FLOAT,
		PL_V2OvervoltagesN03	FLOAT,
		PL_V2OvervoltagesN04	FLOAT,
		PL_V2OvervoltagesN05	FLOAT,
		PL_V2OvervoltagesN06	FLOAT,
		PL_V2OvervoltagesN07	FLOAT,
		PL_V2OvervoltagesN08	FLOAT,
		PL_V2OvervoltagesN09	FLOAT,
		PL_V2OvervoltagesN10	FLOAT,
		PL_V2OvervoltagesN11	FLOAT,
		PL_V2OvervoltagesN12	FLOAT,
		PL_V2OvervoltagesN13	FLOAT,
		PL_V2OvervoltagesN14	FLOAT,
		PL_V2OvervoltagesN15	FLOAT,
		PL_V2OvervoltagesN16	FLOAT,
		PL_V2OvervoltagesN17	FLOAT,
		PL_V2OvervoltagesN18	FLOAT,
		PL_V2OvervoltagesN19	FLOAT,
		PL_V2OvervoltagesN20	FLOAT,
		PL_V2OvervoltagesN21	FLOAT,
		PL_V2OvervoltagesN22	FLOAT,
		PL_V2OvervoltagesN23	FLOAT,
		PL_V2OvervoltagesN24	FLOAT,
		PL_V2OvervoltagesN25	FLOAT,
		PL_V2OvervoltagesN26	FLOAT,
		PL_V2OvervoltagesN27	FLOAT,
		PL_V2OvervoltagesN28	FLOAT,
		PL_V2OvervoltagesN29	FLOAT,
		PL_V2OvervoltagesN30	FLOAT,
		PL_V2OvervoltagesN31	FLOAT,
		PL_V2OvervoltagesN32	FLOAT,
		PL_V2OvervoltagesN33	FLOAT,
		PL_V2OvervoltagesN34	FLOAT,
		PL_V2OvervoltagesN35	FLOAT,
		PL_V2OvervoltagesN36	FLOAT,
		PL_V2OvervoltagesN37	FLOAT,
		PL_V2OvervoltagesN38	FLOAT,
		PL_V2OvervoltagesN39	FLOAT,
		PL_V2OvervoltagesN40	FLOAT,
		PL_V2OvervoltagesN41	FLOAT,
		PL_V2OvervoltagesN42	FLOAT,
		PL_V2OvervoltagesN43	FLOAT,
		PL_V2OvervoltagesN44	FLOAT,
		PL_V2OvervoltagesN45	FLOAT,
		PL_V2OvervoltagesN46	FLOAT,
		PL_V2OvervoltagesN47	FLOAT,
		PL_V2OvervoltagesN48	FLOAT,
		PL_V2OvervoltagesN49	FLOAT,
		PL_V2OvervoltagesN50	FLOAT,
		PL_V2OvervoltagesN51	FLOAT,
		PL_V2OvervoltagesN52	FLOAT,
		PL_V2OvervoltagesN53	FLOAT,
		PL_V2OvervoltagesN54	FLOAT,
		PL_V2OvervoltagesN55	FLOAT,
		PL_V2OvervoltagesN56	FLOAT,
		PL_V2OvervoltagesN57	FLOAT,
		PL_V2OvervoltagesN58	FLOAT,
		PL_V2OvervoltagesN59	FLOAT,
		PL_V2OvervoltagesN60	FLOAT,
		PL_V2OvervoltagesN61	FLOAT,
		PL_V2OvervoltagesN62	FLOAT,
		PL_V2OvervoltagesN63	FLOAT,
		PL_V2OvervoltagesN64	FLOAT,
		PL_V2OvervoltagesN65	FLOAT,
		PL_V2OvervoltagesN66	FLOAT,
		PL_V2OvervoltagesN67	FLOAT,
		PL_V2OvervoltagesN68	FLOAT,
		PL_V2OvervoltagesN69	FLOAT,
		PL_V2OvervoltagesN70	FLOAT,
		PL_V2OvervoltagesN71	FLOAT,
		PL_V2OvervoltagesN72	FLOAT,
		PL_V2OvervoltagesN73	FLOAT,
		PL_V2OvervoltagesN74	FLOAT,
		PL_V2OvervoltagesN75	FLOAT,
		PL_V2OvervoltagesN76	FLOAT,
		PL_V2OvervoltagesN77	FLOAT,
		PL_V2OvervoltagesN78	FLOAT,
		PL_V2OvervoltagesN79	FLOAT,
		PL_V2OvervoltagesN80	FLOAT,
		PL_V2OvervoltagesN81	FLOAT,
		PL_V2OvervoltagesN82	FLOAT,
		PL_V2OvervoltagesN83	FLOAT,
		PL_V2OvervoltagesN84	FLOAT,
		PL_V2OvervoltagesN85	FLOAT,
		PL_V2OvervoltagesN86	FLOAT,
		PL_V2OvervoltagesN87	FLOAT,
		PL_V2OvervoltagesN88	FLOAT,
		PL_V2OvervoltagesN89	FLOAT,
		PL_V2OvervoltagesN90	FLOAT,
		PL_V2OvervoltagesN91	FLOAT,
		PL_V2OvervoltagesN92	FLOAT,
		PL_V2OvervoltagesN93	FLOAT,
		PL_V2OvervoltagesN94	FLOAT,
		PL_V2OvervoltagesN95	FLOAT,
		PL_V2OvervoltagesN96	FLOAT,
		PL_V3OvervoltagesN01	FLOAT,
		PL_V3OvervoltagesN02	FLOAT,
		PL_V3OvervoltagesN03	FLOAT,
		PL_V3OvervoltagesN04	FLOAT,
		PL_V3OvervoltagesN05	FLOAT,
		PL_V3OvervoltagesN06	FLOAT,
		PL_V3OvervoltagesN07	FLOAT,
		PL_V3OvervoltagesN08	FLOAT,
		PL_V3OvervoltagesN09	FLOAT,
		PL_V3OvervoltagesN10	FLOAT,
		PL_V3OvervoltagesN11	FLOAT,
		PL_V3OvervoltagesN12	FLOAT,
		PL_V3OvervoltagesN13	FLOAT,
		PL_V3OvervoltagesN14	FLOAT,
		PL_V3OvervoltagesN15	FLOAT,
		PL_V3OvervoltagesN16	FLOAT,
		PL_V3OvervoltagesN17	FLOAT,
		PL_V3OvervoltagesN18	FLOAT,
		PL_V3OvervoltagesN19	FLOAT,
		PL_V3OvervoltagesN20	FLOAT,
		PL_V3OvervoltagesN21	FLOAT,
		PL_V3OvervoltagesN22	FLOAT,
		PL_V3OvervoltagesN23	FLOAT,
		PL_V3OvervoltagesN24	FLOAT,
		PL_V3OvervoltagesN25	FLOAT,
		PL_V3OvervoltagesN26	FLOAT,
		PL_V3OvervoltagesN27	FLOAT,
		PL_V3OvervoltagesN28	FLOAT,
		PL_V3OvervoltagesN29	FLOAT,
		PL_V3OvervoltagesN30	FLOAT,
		PL_V3OvervoltagesN31	FLOAT,
		PL_V3OvervoltagesN32	FLOAT,
		PL_V3OvervoltagesN33	FLOAT,
		PL_V3OvervoltagesN34	FLOAT,
		PL_V3OvervoltagesN35	FLOAT,
		PL_V3OvervoltagesN36	FLOAT,
		PL_V3OvervoltagesN37	FLOAT,
		PL_V3OvervoltagesN38	FLOAT,
		PL_V3OvervoltagesN39	FLOAT,
		PL_V3OvervoltagesN40	FLOAT,
		PL_V3OvervoltagesN41	FLOAT,
		PL_V3OvervoltagesN42	FLOAT,
		PL_V3OvervoltagesN43	FLOAT,
		PL_V3OvervoltagesN44	FLOAT,
		PL_V3OvervoltagesN45	FLOAT,
		PL_V3OvervoltagesN46	FLOAT,
		PL_V3OvervoltagesN47	FLOAT,
		PL_V3OvervoltagesN48	FLOAT,
		PL_V3OvervoltagesN49	FLOAT,
		PL_V3OvervoltagesN50	FLOAT,
		PL_V3OvervoltagesN51	FLOAT,
		PL_V3OvervoltagesN52	FLOAT,
		PL_V3OvervoltagesN53	FLOAT,
		PL_V3OvervoltagesN54	FLOAT,
		PL_V3OvervoltagesN55	FLOAT,
		PL_V3OvervoltagesN56	FLOAT,
		PL_V3OvervoltagesN57	FLOAT,
		PL_V3OvervoltagesN58	FLOAT,
		PL_V3OvervoltagesN59	FLOAT,
		PL_V3OvervoltagesN60	FLOAT,
		PL_V3OvervoltagesN61	FLOAT,
		PL_V3OvervoltagesN62	FLOAT,
		PL_V3OvervoltagesN63	FLOAT,
		PL_V3OvervoltagesN64	FLOAT,
		PL_V3OvervoltagesN65	FLOAT,
		PL_V3OvervoltagesN66	FLOAT,
		PL_V3OvervoltagesN67	FLOAT,
		PL_V3OvervoltagesN68	FLOAT,
		PL_V3OvervoltagesN69	FLOAT,
		PL_V3OvervoltagesN70	FLOAT,
		PL_V3OvervoltagesN71	FLOAT,
		PL_V3OvervoltagesN72	FLOAT,
		PL_V3OvervoltagesN73	FLOAT,
		PL_V3OvervoltagesN74	FLOAT,
		PL_V3OvervoltagesN75	FLOAT,
		PL_V3OvervoltagesN76	FLOAT,
		PL_V3OvervoltagesN77	FLOAT,
		PL_V3OvervoltagesN78	FLOAT,
		PL_V3OvervoltagesN79	FLOAT,
		PL_V3OvervoltagesN80	FLOAT,
		PL_V3OvervoltagesN81	FLOAT,
		PL_V3OvervoltagesN82	FLOAT,
		PL_V3OvervoltagesN83	FLOAT,
		PL_V3OvervoltagesN84	FLOAT,
		PL_V3OvervoltagesN85	FLOAT,
		PL_V3OvervoltagesN86	FLOAT,
		PL_V3OvervoltagesN87	FLOAT,
		PL_V3OvervoltagesN88	FLOAT,
		PL_V3OvervoltagesN89	FLOAT,
		PL_V3OvervoltagesN90	FLOAT,
		PL_V3OvervoltagesN91	FLOAT,
		PL_V3OvervoltagesN92	FLOAT,
		PL_V3OvervoltagesN93	FLOAT,
		PL_V3OvervoltagesN94	FLOAT,
		PL_V3OvervoltagesN95	FLOAT,
		PL_V3OvervoltagesN96	FLOAT
	)
	 
	-- put the raw data in a form that's easier to query
	DECLARE @TblData TABLE
	(
		SourceID		INT,
		MeasurementName	NVARCHAR(200),
		TimestampUtc	DATETIME,
		Value			FLOAT
	)

	-- insert data for the PowerLogic (PL) devices into the staging table
	INSERT INTO
		@TblData
	SELECT
		s.ID AS SourceID,
		q.Name AS MeasurementName,
		dl.TimestampUTC AS TimestampUtc,
		dl.Value
	FROM
		dbo.DataLog2 dl
		INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
		INNER JOIN @TblSources s on dl.SourceID = s.ID
	WHERE
		s.MeasurementSetIdentifier = N'PL'
		AND q.Name IN
		(
			N'V1 Overvoltage N01',
			N'V1 Overvoltage N02',
			N'V1 Overvoltage N03',
			N'V1 Overvoltage N04',
			N'V1 Overvoltage N05',
			N'V1 Overvoltage N06',
			N'V1 Overvoltage N07',
			N'V1 Overvoltage N08',
			N'V1 Overvoltage N09',
			N'V1 Overvoltage N10',
			N'V1 Overvoltage N11',
			N'V1 Overvoltage N12',
			N'V1 Overvoltage N13',
			N'V1 Overvoltage N14',
			N'V1 Overvoltage N15',
			N'V1 Overvoltage N16',
			N'V1 Overvoltage N17',
			N'V1 Overvoltage N18',
			N'V1 Overvoltage N19',
			N'V1 Overvoltage N20',
			N'V1 Overvoltage N21',
			N'V1 Overvoltage N22',
			N'V1 Overvoltage N23',
			N'V1 Overvoltage N24',
			N'V1 Overvoltage N25',
			N'V1 Overvoltage N26',
			N'V1 Overvoltage N27',
			N'V1 Overvoltage N28',
			N'V1 Overvoltage N29',
			N'V1 Overvoltage N30',
			N'V1 Overvoltage N31',
			N'V1 Overvoltage N32',
			N'V1 Overvoltage N33',
			N'V1 Overvoltage N34',
			N'V1 Overvoltage N35',
			N'V1 Overvoltage N36',
			N'V1 Overvoltage N37',
			N'V1 Overvoltage N38',
			N'V1 Overvoltage N39',
			N'V1 Overvoltage N40',
			N'V1 Overvoltage N41',
			N'V1 Overvoltage N42',
			N'V1 Overvoltage N43',
			N'V1 Overvoltage N44',
			N'V1 Overvoltage N45',
			N'V1 Overvoltage N46',
			N'V1 Overvoltage N47',
			N'V1 Overvoltage N48',
			N'V1 Overvoltage N49',
			N'V1 Overvoltage N50',
			N'V1 Overvoltage N51',
			N'V1 Overvoltage N52',
			N'V1 Overvoltage N53',
			N'V1 Overvoltage N54',
			N'V1 Overvoltage N55',
			N'V1 Overvoltage N56',
			N'V1 Overvoltage N57',
			N'V1 Overvoltage N58',
			N'V1 Overvoltage N59',
			N'V1 Overvoltage N60',
			N'V1 Overvoltage N61',
			N'V1 Overvoltage N62',
			N'V1 Overvoltage N63',
			N'V1 Overvoltage N64',
			N'V1 Overvoltage N65',
			N'V1 Overvoltage N66',
			N'V1 Overvoltage N67',
			N'V1 Overvoltage N68',
			N'V1 Overvoltage N69',
			N'V1 Overvoltage N70',
			N'V1 Overvoltage N71',
			N'V1 Overvoltage N72',
			N'V1 Overvoltage N73',
			N'V1 Overvoltage N74',
			N'V1 Overvoltage N75',
			N'V1 Overvoltage N76',
			N'V1 Overvoltage N77',
			N'V1 Overvoltage N78',
			N'V1 Overvoltage N79',
			N'V1 Overvoltage N80',
			N'V1 Overvoltage N81',
			N'V1 Overvoltage N82',
			N'V1 Overvoltage N83',
			N'V1 Overvoltage N84',
			N'V1 Overvoltage N85',
			N'V1 Overvoltage N86',
			N'V1 Overvoltage N87',
			N'V1 Overvoltage N88',
			N'V1 Overvoltage N89',
			N'V1 Overvoltage N90',
			N'V1 Overvoltage N91',
			N'V1 Overvoltage N92',
			N'V1 Overvoltage N93',
			N'V1 Overvoltage N94',
			N'V1 Overvoltage N95',
			N'V1 Overvoltage N96',
			N'V2 Overvoltage N01',
			N'V2 Overvoltage N02',
			N'V2 Overvoltage N03',
			N'V2 Overvoltage N04',
			N'V2 Overvoltage N05',
			N'V2 Overvoltage N06',
			N'V2 Overvoltage N07',
			N'V2 Overvoltage N08',
			N'V2 Overvoltage N09',
			N'V2 Overvoltage N10',
			N'V2 Overvoltage N11',
			N'V2 Overvoltage N12',
			N'V2 Overvoltage N13',
			N'V2 Overvoltage N14',
			N'V2 Overvoltage N15',
			N'V2 Overvoltage N16',
			N'V2 Overvoltage N17',
			N'V2 Overvoltage N18',
			N'V2 Overvoltage N19',
			N'V2 Overvoltage N20',
			N'V2 Overvoltage N21',
			N'V2 Overvoltage N22',
			N'V2 Overvoltage N23',
			N'V2 Overvoltage N24',
			N'V2 Overvoltage N25',
			N'V2 Overvoltage N26',
			N'V2 Overvoltage N27',
			N'V2 Overvoltage N28',
			N'V2 Overvoltage N29',
			N'V2 Overvoltage N30',
			N'V2 Overvoltage N31',
			N'V2 Overvoltage N32',
			N'V2 Overvoltage N33',
			N'V2 Overvoltage N34',
			N'V2 Overvoltage N35',
			N'V2 Overvoltage N36',
			N'V2 Overvoltage N37',
			N'V2 Overvoltage N38',
			N'V2 Overvoltage N39',
			N'V2 Overvoltage N40',
			N'V2 Overvoltage N41',
			N'V2 Overvoltage N42',
			N'V2 Overvoltage N43',
			N'V2 Overvoltage N44',
			N'V2 Overvoltage N45',
			N'V2 Overvoltage N46',
			N'V2 Overvoltage N47',
			N'V2 Overvoltage N48',
			N'V2 Overvoltage N49',
			N'V2 Overvoltage N50',
			N'V2 Overvoltage N51',
			N'V2 Overvoltage N52',
			N'V2 Overvoltage N53',
			N'V2 Overvoltage N54',
			N'V2 Overvoltage N55',
			N'V2 Overvoltage N56',
			N'V2 Overvoltage N57',
			N'V2 Overvoltage N58',
			N'V2 Overvoltage N59',
			N'V2 Overvoltage N60',
			N'V2 Overvoltage N61',
			N'V2 Overvoltage N62',
			N'V2 Overvoltage N63',
			N'V2 Overvoltage N64',
			N'V2 Overvoltage N65',
			N'V2 Overvoltage N66',
			N'V2 Overvoltage N67',
			N'V2 Overvoltage N68',
			N'V2 Overvoltage N69',
			N'V2 Overvoltage N70',
			N'V2 Overvoltage N71',
			N'V2 Overvoltage N72',
			N'V2 Overvoltage N73',
			N'V2 Overvoltage N74',
			N'V2 Overvoltage N75',
			N'V2 Overvoltage N76',
			N'V2 Overvoltage N77',
			N'V2 Overvoltage N78',
			N'V2 Overvoltage N79',
			N'V2 Overvoltage N80',
			N'V2 Overvoltage N81',
			N'V2 Overvoltage N82',
			N'V2 Overvoltage N83',
			N'V2 Overvoltage N84',
			N'V2 Overvoltage N85',
			N'V2 Overvoltage N86',
			N'V2 Overvoltage N87',
			N'V2 Overvoltage N88',
			N'V2 Overvoltage N89',
			N'V2 Overvoltage N90',
			N'V2 Overvoltage N91',
			N'V2 Overvoltage N92',
			N'V2 Overvoltage N93',
			N'V2 Overvoltage N94',
			N'V2 Overvoltage N95',
			N'V2 Overvoltage N96',
			N'V3 Overvoltage N01',
			N'V3 Overvoltage N02',
			N'V3 Overvoltage N03',
			N'V3 Overvoltage N04',
			N'V3 Overvoltage N05',
			N'V3 Overvoltage N06',
			N'V3 Overvoltage N07',
			N'V3 Overvoltage N08',
			N'V3 Overvoltage N09',
			N'V3 Overvoltage N10',
			N'V3 Overvoltage N11',
			N'V3 Overvoltage N12',
			N'V3 Overvoltage N13',
			N'V3 Overvoltage N14',
			N'V3 Overvoltage N15',
			N'V3 Overvoltage N16',
			N'V3 Overvoltage N17',
			N'V3 Overvoltage N18',
			N'V3 Overvoltage N19',
			N'V3 Overvoltage N20',
			N'V3 Overvoltage N21',
			N'V3 Overvoltage N22',
			N'V3 Overvoltage N23',
			N'V3 Overvoltage N24',
			N'V3 Overvoltage N25',
			N'V3 Overvoltage N26',
			N'V3 Overvoltage N27',
			N'V3 Overvoltage N28',
			N'V3 Overvoltage N29',
			N'V3 Overvoltage N30',
			N'V3 Overvoltage N31',
			N'V3 Overvoltage N32',
			N'V3 Overvoltage N33',
			N'V3 Overvoltage N34',
			N'V3 Overvoltage N35',
			N'V3 Overvoltage N36',
			N'V3 Overvoltage N37',
			N'V3 Overvoltage N38',
			N'V3 Overvoltage N39',
			N'V3 Overvoltage N40',
			N'V3 Overvoltage N41',
			N'V3 Overvoltage N42',
			N'V3 Overvoltage N43',
			N'V3 Overvoltage N44',
			N'V3 Overvoltage N45',
			N'V3 Overvoltage N46',
			N'V3 Overvoltage N47',
			N'V3 Overvoltage N48',
			N'V3 Overvoltage N49',
			N'V3 Overvoltage N50',
			N'V3 Overvoltage N51',
			N'V3 Overvoltage N52',
			N'V3 Overvoltage N53',
			N'V3 Overvoltage N54',
			N'V3 Overvoltage N55',
			N'V3 Overvoltage N56',
			N'V3 Overvoltage N57',
			N'V3 Overvoltage N58',
			N'V3 Overvoltage N59',
			N'V3 Overvoltage N60',
			N'V3 Overvoltage N61',
			N'V3 Overvoltage N62',
			N'V3 Overvoltage N63',
			N'V3 Overvoltage N64',
			N'V3 Overvoltage N65',
			N'V3 Overvoltage N66',
			N'V3 Overvoltage N67',
			N'V3 Overvoltage N68',
			N'V3 Overvoltage N69',
			N'V3 Overvoltage N70',
			N'V3 Overvoltage N71',
			N'V3 Overvoltage N72',
			N'V3 Overvoltage N73',
			N'V3 Overvoltage N74',
			N'V3 Overvoltage N75',
			N'V3 Overvoltage N76',
			N'V3 Overvoltage N77',
			N'V3 Overvoltage N78',
			N'V3 Overvoltage N79',
			N'V3 Overvoltage N80',
			N'V3 Overvoltage N81',
			N'V3 Overvoltage N82',
			N'V3 Overvoltage N83',
			N'V3 Overvoltage N84',
			N'V3 Overvoltage N85',
			N'V3 Overvoltage N86',
			N'V3 Overvoltage N87',
			N'V3 Overvoltage N88',
			N'V3 Overvoltage N89',
			N'V3 Overvoltage N90',
			N'V3 Overvoltage N91',
			N'V3 Overvoltage N92',
			N'V3 Overvoltage N93',
			N'V3 Overvoltage N94',
			N'V3 Overvoltage N95',
			N'V3 Overvoltage N96'
		)
		AND EXISTS
		(
			SELECT
				1
			FROM
				dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
			WHERE
				dl.TimestampUTC > ts.StartDate 
				AND dl.TimestampUTC <= ts.EndDate
		)
	ORDER BY
		TimestampUTC,
		SourceID,
		q.Name

	-- select data for the PowerLogic (PL) devices into the results table
	SELECT
		TimestampUtc,
		SourceID,
		[V1 Overvoltage N01] AS PL_V1OvervoltagesN01,
		[V1 Overvoltage N02] AS PL_V1OvervoltagesN02,
		[V1 Overvoltage N03] AS PL_V1OvervoltagesN03,
		[V1 Overvoltage N04] AS PL_V1OvervoltagesN04,
		[V1 Overvoltage N05] AS PL_V1OvervoltagesN05,
		[V1 Overvoltage N06] AS PL_V1OvervoltagesN06,
		[V1 Overvoltage N07] AS PL_V1OvervoltagesN07,
		[V1 Overvoltage N08] AS PL_V1OvervoltagesN08,
		[V1 Overvoltage N09] AS PL_V1OvervoltagesN09,
		[V1 Overvoltage N10] AS PL_V1OvervoltagesN10,
		[V1 Overvoltage N11] AS PL_V1OvervoltagesN11,
		[V1 Overvoltage N12] AS PL_V1OvervoltagesN12,
		[V1 Overvoltage N13] AS PL_V1OvervoltagesN13,
		[V1 Overvoltage N14] AS PL_V1OvervoltagesN14,
		[V1 Overvoltage N15] AS PL_V1OvervoltagesN15,
		[V1 Overvoltage N16] AS PL_V1OvervoltagesN16,
		[V1 Overvoltage N17] AS PL_V1OvervoltagesN17,
		[V1 Overvoltage N18] AS PL_V1OvervoltagesN18,
		[V1 Overvoltage N19] AS PL_V1OvervoltagesN19,
		[V1 Overvoltage N20] AS PL_V1OvervoltagesN20,
		[V1 Overvoltage N21] AS PL_V1OvervoltagesN21,
		[V1 Overvoltage N22] AS PL_V1OvervoltagesN22,
		[V1 Overvoltage N23] AS PL_V1OvervoltagesN23,
		[V1 Overvoltage N24] AS PL_V1OvervoltagesN24,
		[V1 Overvoltage N25] AS PL_V1OvervoltagesN25,
		[V1 Overvoltage N26] AS PL_V1OvervoltagesN26,
		[V1 Overvoltage N27] AS PL_V1OvervoltagesN27,
		[V1 Overvoltage N28] AS PL_V1OvervoltagesN28,
		[V1 Overvoltage N29] AS PL_V1OvervoltagesN29,
		[V1 Overvoltage N30] AS PL_V1OvervoltagesN30,
		[V1 Overvoltage N31] AS PL_V1OvervoltagesN31,
		[V1 Overvoltage N32] AS PL_V1OvervoltagesN32,
		[V1 Overvoltage N33] AS PL_V1OvervoltagesN33,
		[V1 Overvoltage N34] AS PL_V1OvervoltagesN34,
		[V1 Overvoltage N35] AS PL_V1OvervoltagesN35,
		[V1 Overvoltage N36] AS PL_V1OvervoltagesN36,
		[V1 Overvoltage N37] AS PL_V1OvervoltagesN37,
		[V1 Overvoltage N38] AS PL_V1OvervoltagesN38,
		[V1 Overvoltage N39] AS PL_V1OvervoltagesN39,
		[V1 Overvoltage N40] AS PL_V1OvervoltagesN40,
		[V1 Overvoltage N41] AS PL_V1OvervoltagesN41,
		[V1 Overvoltage N42] AS PL_V1OvervoltagesN42,
		[V1 Overvoltage N43] AS PL_V1OvervoltagesN43,
		[V1 Overvoltage N44] AS PL_V1OvervoltagesN44,
		[V1 Overvoltage N45] AS PL_V1OvervoltagesN45,
		[V1 Overvoltage N46] AS PL_V1OvervoltagesN46,
		[V1 Overvoltage N47] AS PL_V1OvervoltagesN47,
		[V1 Overvoltage N48] AS PL_V1OvervoltagesN48,
		[V1 Overvoltage N49] AS PL_V1OvervoltagesN49,
		[V1 Overvoltage N50] AS PL_V1OvervoltagesN50,
		[V1 Overvoltage N51] AS PL_V1OvervoltagesN51,
		[V1 Overvoltage N52] AS PL_V1OvervoltagesN52,
		[V1 Overvoltage N53] AS PL_V1OvervoltagesN53,
		[V1 Overvoltage N54] AS PL_V1OvervoltagesN54,
		[V1 Overvoltage N55] AS PL_V1OvervoltagesN55,
		[V1 Overvoltage N56] AS PL_V1OvervoltagesN56,
		[V1 Overvoltage N57] AS PL_V1OvervoltagesN57,
		[V1 Overvoltage N58] AS PL_V1OvervoltagesN58,
		[V1 Overvoltage N59] AS PL_V1OvervoltagesN59,
		[V1 Overvoltage N60] AS PL_V1OvervoltagesN60,
		[V1 Overvoltage N61] AS PL_V1OvervoltagesN61,
		[V1 Overvoltage N62] AS PL_V1OvervoltagesN62,
		[V1 Overvoltage N63] AS PL_V1OvervoltagesN63,
		[V1 Overvoltage N64] AS PL_V1OvervoltagesN64,
		[V1 Overvoltage N65] AS PL_V1OvervoltagesN65,
		[V1 Overvoltage N66] AS PL_V1OvervoltagesN66,
		[V1 Overvoltage N67] AS PL_V1OvervoltagesN67,
		[V1 Overvoltage N68] AS PL_V1OvervoltagesN68,
		[V1 Overvoltage N69] AS PL_V1OvervoltagesN69,
		[V1 Overvoltage N70] AS PL_V1OvervoltagesN70,
		[V1 Overvoltage N71] AS PL_V1OvervoltagesN71,
		[V1 Overvoltage N72] AS PL_V1OvervoltagesN72,
		[V1 Overvoltage N73] AS PL_V1OvervoltagesN73,
		[V1 Overvoltage N74] AS PL_V1OvervoltagesN74,
		[V1 Overvoltage N75] AS PL_V1OvervoltagesN75,
		[V1 Overvoltage N76] AS PL_V1OvervoltagesN76,
		[V1 Overvoltage N77] AS PL_V1OvervoltagesN77,
		[V1 Overvoltage N78] AS PL_V1OvervoltagesN78,
		[V1 Overvoltage N79] AS PL_V1OvervoltagesN79,
		[V1 Overvoltage N80] AS PL_V1OvervoltagesN80,
		[V1 Overvoltage N81] AS PL_V1OvervoltagesN81,
		[V1 Overvoltage N82] AS PL_V1OvervoltagesN82,
		[V1 Overvoltage N83] AS PL_V1OvervoltagesN83,
		[V1 Overvoltage N84] AS PL_V1OvervoltagesN84,
		[V1 Overvoltage N85] AS PL_V1OvervoltagesN85,
		[V1 Overvoltage N86] AS PL_V1OvervoltagesN86,
		[V1 Overvoltage N87] AS PL_V1OvervoltagesN87,
		[V1 Overvoltage N88] AS PL_V1OvervoltagesN88,
		[V1 Overvoltage N89] AS PL_V1OvervoltagesN89,
		[V1 Overvoltage N90] AS PL_V1OvervoltagesN90,
		[V1 Overvoltage N91] AS PL_V1OvervoltagesN91,
		[V1 Overvoltage N92] AS PL_V1OvervoltagesN92,
		[V1 Overvoltage N93] AS PL_V1OvervoltagesN93,
		[V1 Overvoltage N94] AS PL_V1OvervoltagesN94,
		[V1 Overvoltage N95] AS PL_V1OvervoltagesN95,
		[V1 Overvoltage N96] AS PL_V1OvervoltagesN96,
		[V2 Overvoltage N01] AS PL_V2OvervoltagesN01,
		[V2 Overvoltage N02] AS PL_V2OvervoltagesN02,
		[V2 Overvoltage N03] AS PL_V2OvervoltagesN03,
		[V2 Overvoltage N04] AS PL_V2OvervoltagesN04,
		[V2 Overvoltage N05] AS PL_V2OvervoltagesN05,
		[V2 Overvoltage N06] AS PL_V2OvervoltagesN06,
		[V2 Overvoltage N07] AS PL_V2OvervoltagesN07,
		[V2 Overvoltage N08] AS PL_V2OvervoltagesN08,
		[V2 Overvoltage N09] AS PL_V2OvervoltagesN09,
		[V2 Overvoltage N10] AS PL_V2OvervoltagesN10,
		[V2 Overvoltage N11] AS PL_V2OvervoltagesN11,
		[V2 Overvoltage N12] AS PL_V2OvervoltagesN12,
		[V2 Overvoltage N13] AS PL_V2OvervoltagesN13,
		[V2 Overvoltage N14] AS PL_V2OvervoltagesN14,
		[V2 Overvoltage N15] AS PL_V2OvervoltagesN15,
		[V2 Overvoltage N16] AS PL_V2OvervoltagesN16,
		[V2 Overvoltage N17] AS PL_V2OvervoltagesN17,
		[V2 Overvoltage N18] AS PL_V2OvervoltagesN18,
		[V2 Overvoltage N19] AS PL_V2OvervoltagesN19,
		[V2 Overvoltage N20] AS PL_V2OvervoltagesN20,
		[V2 Overvoltage N21] AS PL_V2OvervoltagesN21,
		[V2 Overvoltage N22] AS PL_V2OvervoltagesN22,
		[V2 Overvoltage N23] AS PL_V2OvervoltagesN23,
		[V2 Overvoltage N24] AS PL_V2OvervoltagesN24,
		[V2 Overvoltage N25] AS PL_V2OvervoltagesN25,
		[V2 Overvoltage N26] AS PL_V2OvervoltagesN26,
		[V2 Overvoltage N27] AS PL_V2OvervoltagesN27,
		[V2 Overvoltage N28] AS PL_V2OvervoltagesN28,
		[V2 Overvoltage N29] AS PL_V2OvervoltagesN29,
		[V2 Overvoltage N30] AS PL_V2OvervoltagesN30,
		[V2 Overvoltage N31] AS PL_V2OvervoltagesN31,
		[V2 Overvoltage N32] AS PL_V2OvervoltagesN32,
		[V2 Overvoltage N33] AS PL_V2OvervoltagesN33,
		[V2 Overvoltage N34] AS PL_V2OvervoltagesN34,
		[V2 Overvoltage N35] AS PL_V2OvervoltagesN35,
		[V2 Overvoltage N36] AS PL_V2OvervoltagesN36,
		[V2 Overvoltage N37] AS PL_V2OvervoltagesN37,
		[V2 Overvoltage N38] AS PL_V2OvervoltagesN38,
		[V2 Overvoltage N39] AS PL_V2OvervoltagesN39,
		[V2 Overvoltage N40] AS PL_V2OvervoltagesN40,
		[V2 Overvoltage N41] AS PL_V2OvervoltagesN41,
		[V2 Overvoltage N42] AS PL_V2OvervoltagesN42,
		[V2 Overvoltage N43] AS PL_V2OvervoltagesN43,
		[V2 Overvoltage N44] AS PL_V2OvervoltagesN44,
		[V2 Overvoltage N45] AS PL_V2OvervoltagesN45,
		[V2 Overvoltage N46] AS PL_V2OvervoltagesN46,
		[V2 Overvoltage N47] AS PL_V2OvervoltagesN47,
		[V2 Overvoltage N48] AS PL_V2OvervoltagesN48,
		[V2 Overvoltage N49] AS PL_V2OvervoltagesN49,
		[V2 Overvoltage N50] AS PL_V2OvervoltagesN50,
		[V2 Overvoltage N51] AS PL_V2OvervoltagesN51,
		[V2 Overvoltage N52] AS PL_V2OvervoltagesN52,
		[V2 Overvoltage N53] AS PL_V2OvervoltagesN53,
		[V2 Overvoltage N54] AS PL_V2OvervoltagesN54,
		[V2 Overvoltage N55] AS PL_V2OvervoltagesN55,
		[V2 Overvoltage N56] AS PL_V2OvervoltagesN56,
		[V2 Overvoltage N57] AS PL_V2OvervoltagesN57,
		[V2 Overvoltage N58] AS PL_V2OvervoltagesN58,
		[V2 Overvoltage N59] AS PL_V2OvervoltagesN59,
		[V2 Overvoltage N60] AS PL_V2OvervoltagesN60,
		[V2 Overvoltage N61] AS PL_V2OvervoltagesN61,
		[V2 Overvoltage N62] AS PL_V2OvervoltagesN62,
		[V2 Overvoltage N63] AS PL_V2OvervoltagesN63,
		[V2 Overvoltage N64] AS PL_V2OvervoltagesN64,
		[V2 Overvoltage N65] AS PL_V2OvervoltagesN65,
		[V2 Overvoltage N66] AS PL_V2OvervoltagesN66,
		[V2 Overvoltage N67] AS PL_V2OvervoltagesN67,
		[V2 Overvoltage N68] AS PL_V2OvervoltagesN68,
		[V2 Overvoltage N69] AS PL_V2OvervoltagesN69,
		[V2 Overvoltage N70] AS PL_V2OvervoltagesN70,
		[V2 Overvoltage N71] AS PL_V2OvervoltagesN71,
		[V2 Overvoltage N72] AS PL_V2OvervoltagesN72,
		[V2 Overvoltage N73] AS PL_V2OvervoltagesN73,
		[V2 Overvoltage N74] AS PL_V2OvervoltagesN74,
		[V2 Overvoltage N75] AS PL_V2OvervoltagesN75,
		[V2 Overvoltage N76] AS PL_V2OvervoltagesN76,
		[V2 Overvoltage N77] AS PL_V2OvervoltagesN77,
		[V2 Overvoltage N78] AS PL_V2OvervoltagesN78,
		[V2 Overvoltage N79] AS PL_V2OvervoltagesN79,
		[V2 Overvoltage N80] AS PL_V2OvervoltagesN80,
		[V2 Overvoltage N81] AS PL_V2OvervoltagesN81,
		[V2 Overvoltage N82] AS PL_V2OvervoltagesN82,
		[V2 Overvoltage N83] AS PL_V2OvervoltagesN83,
		[V2 Overvoltage N84] AS PL_V2OvervoltagesN84,
		[V2 Overvoltage N85] AS PL_V2OvervoltagesN85,
		[V2 Overvoltage N86] AS PL_V2OvervoltagesN86,
		[V2 Overvoltage N87] AS PL_V2OvervoltagesN87,
		[V2 Overvoltage N88] AS PL_V2OvervoltagesN88,
		[V2 Overvoltage N89] AS PL_V2OvervoltagesN89,
		[V2 Overvoltage N90] AS PL_V2OvervoltagesN90,
		[V2 Overvoltage N91] AS PL_V2OvervoltagesN91,
		[V2 Overvoltage N92] AS PL_V2OvervoltagesN92,
		[V2 Overvoltage N93] AS PL_V2OvervoltagesN93,
		[V2 Overvoltage N94] AS PL_V2OvervoltagesN94,
		[V2 Overvoltage N95] AS PL_V2OvervoltagesN95,
		[V2 Overvoltage N96] AS PL_V2OvervoltagesN96,
		[V3 Overvoltage N01] AS PL_V3OvervoltagesN01,
		[V3 Overvoltage N02] AS PL_V3OvervoltagesN02,
		[V3 Overvoltage N03] AS PL_V3OvervoltagesN03,
		[V3 Overvoltage N04] AS PL_V3OvervoltagesN04,
		[V3 Overvoltage N05] AS PL_V3OvervoltagesN05,
		[V3 Overvoltage N06] AS PL_V3OvervoltagesN06,
		[V3 Overvoltage N07] AS PL_V3OvervoltagesN07,
		[V3 Overvoltage N08] AS PL_V3OvervoltagesN08,
		[V3 Overvoltage N09] AS PL_V3OvervoltagesN09,
		[V3 Overvoltage N10] AS PL_V3OvervoltagesN10,
		[V3 Overvoltage N11] AS PL_V3OvervoltagesN11,
		[V3 Overvoltage N12] AS PL_V3OvervoltagesN12,
		[V3 Overvoltage N13] AS PL_V3OvervoltagesN13,
		[V3 Overvoltage N14] AS PL_V3OvervoltagesN14,
		[V3 Overvoltage N15] AS PL_V3OvervoltagesN15,
		[V3 Overvoltage N16] AS PL_V3OvervoltagesN16,
		[V3 Overvoltage N17] AS PL_V3OvervoltagesN17,
		[V3 Overvoltage N18] AS PL_V3OvervoltagesN18,
		[V3 Overvoltage N19] AS PL_V3OvervoltagesN19,
		[V3 Overvoltage N20] AS PL_V3OvervoltagesN20,
		[V3 Overvoltage N21] AS PL_V3OvervoltagesN21,
		[V3 Overvoltage N22] AS PL_V3OvervoltagesN22,
		[V3 Overvoltage N23] AS PL_V3OvervoltagesN23,
		[V3 Overvoltage N24] AS PL_V3OvervoltagesN24,
		[V3 Overvoltage N25] AS PL_V3OvervoltagesN25,
		[V3 Overvoltage N26] AS PL_V3OvervoltagesN26,
		[V3 Overvoltage N27] AS PL_V3OvervoltagesN27,
		[V3 Overvoltage N28] AS PL_V3OvervoltagesN28,
		[V3 Overvoltage N29] AS PL_V3OvervoltagesN29,
		[V3 Overvoltage N30] AS PL_V3OvervoltagesN30,
		[V3 Overvoltage N31] AS PL_V3OvervoltagesN31,
		[V3 Overvoltage N32] AS PL_V3OvervoltagesN32,
		[V3 Overvoltage N33] AS PL_V3OvervoltagesN33,
		[V3 Overvoltage N34] AS PL_V3OvervoltagesN34,
		[V3 Overvoltage N35] AS PL_V3OvervoltagesN35,
		[V3 Overvoltage N36] AS PL_V3OvervoltagesN36,
		[V3 Overvoltage N37] AS PL_V3OvervoltagesN37,
		[V3 Overvoltage N38] AS PL_V3OvervoltagesN38,
		[V3 Overvoltage N39] AS PL_V3OvervoltagesN39,
		[V3 Overvoltage N40] AS PL_V3OvervoltagesN40,
		[V3 Overvoltage N41] AS PL_V3OvervoltagesN41,
		[V3 Overvoltage N42] AS PL_V3OvervoltagesN42,
		[V3 Overvoltage N43] AS PL_V3OvervoltagesN43,
		[V3 Overvoltage N44] AS PL_V3OvervoltagesN44,
		[V3 Overvoltage N45] AS PL_V3OvervoltagesN45,
		[V3 Overvoltage N46] AS PL_V3OvervoltagesN46,
		[V3 Overvoltage N47] AS PL_V3OvervoltagesN47,
		[V3 Overvoltage N48] AS PL_V3OvervoltagesN48,
		[V3 Overvoltage N49] AS PL_V3OvervoltagesN49,
		[V3 Overvoltage N50] AS PL_V3OvervoltagesN50,
		[V3 Overvoltage N51] AS PL_V3OvervoltagesN51,
		[V3 Overvoltage N52] AS PL_V3OvervoltagesN52,
		[V3 Overvoltage N53] AS PL_V3OvervoltagesN53,
		[V3 Overvoltage N54] AS PL_V3OvervoltagesN54,
		[V3 Overvoltage N55] AS PL_V3OvervoltagesN55,
		[V3 Overvoltage N56] AS PL_V3OvervoltagesN56,
		[V3 Overvoltage N57] AS PL_V3OvervoltagesN57,
		[V3 Overvoltage N58] AS PL_V3OvervoltagesN58,
		[V3 Overvoltage N59] AS PL_V3OvervoltagesN59,
		[V3 Overvoltage N60] AS PL_V3OvervoltagesN60,
		[V3 Overvoltage N61] AS PL_V3OvervoltagesN61,
		[V3 Overvoltage N62] AS PL_V3OvervoltagesN62,
		[V3 Overvoltage N63] AS PL_V3OvervoltagesN63,
		[V3 Overvoltage N64] AS PL_V3OvervoltagesN64,
		[V3 Overvoltage N65] AS PL_V3OvervoltagesN65,
		[V3 Overvoltage N66] AS PL_V3OvervoltagesN66,
		[V3 Overvoltage N67] AS PL_V3OvervoltagesN67,
		[V3 Overvoltage N68] AS PL_V3OvervoltagesN68,
		[V3 Overvoltage N69] AS PL_V3OvervoltagesN69,
		[V3 Overvoltage N70] AS PL_V3OvervoltagesN70,
		[V3 Overvoltage N71] AS PL_V3OvervoltagesN71,
		[V3 Overvoltage N72] AS PL_V3OvervoltagesN72,
		[V3 Overvoltage N73] AS PL_V3OvervoltagesN73,
		[V3 Overvoltage N74] AS PL_V3OvervoltagesN74,
		[V3 Overvoltage N75] AS PL_V3OvervoltagesN75,
		[V3 Overvoltage N76] AS PL_V3OvervoltagesN76,
		[V3 Overvoltage N77] AS PL_V3OvervoltagesN77,
		[V3 Overvoltage N78] AS PL_V3OvervoltagesN78,
		[V3 Overvoltage N79] AS PL_V3OvervoltagesN79,
		[V3 Overvoltage N80] AS PL_V3OvervoltagesN80,
		[V3 Overvoltage N81] AS PL_V3OvervoltagesN81,
		[V3 Overvoltage N82] AS PL_V3OvervoltagesN82,
		[V3 Overvoltage N83] AS PL_V3OvervoltagesN83,
		[V3 Overvoltage N84] AS PL_V3OvervoltagesN84,
		[V3 Overvoltage N85] AS PL_V3OvervoltagesN85,
		[V3 Overvoltage N86] AS PL_V3OvervoltagesN86,
		[V3 Overvoltage N87] AS PL_V3OvervoltagesN87,
		[V3 Overvoltage N88] AS PL_V3OvervoltagesN88,
		[V3 Overvoltage N89] AS PL_V3OvervoltagesN89,
		[V3 Overvoltage N90] AS PL_V3OvervoltagesN90,
		[V3 Overvoltage N91] AS PL_V3OvervoltagesN91,
		[V3 Overvoltage N92] AS PL_V3OvervoltagesN92,
		[V3 Overvoltage N93] AS PL_V3OvervoltagesN93,
		[V3 Overvoltage N94] AS PL_V3OvervoltagesN94,
		[V3 Overvoltage N95] AS PL_V3OvervoltagesN95,
		[V3 Overvoltage N96] AS PL_V3OvervoltagesN96
	FROM
		@TblData
	PIVOT
	(
		MAX(Value)
		FOR MeasurementName IN
		(
			[V1 Overvoltage N01],
			[V1 Overvoltage N02],
			[V1 Overvoltage N03],
			[V1 Overvoltage N04],
			[V1 Overvoltage N05],
			[V1 Overvoltage N06],
			[V1 Overvoltage N07],
			[V1 Overvoltage N08],
			[V1 Overvoltage N09],
			[V1 Overvoltage N10],
			[V1 Overvoltage N11],
			[V1 Overvoltage N12],
			[V1 Overvoltage N13],
			[V1 Overvoltage N14],
			[V1 Overvoltage N15],
			[V1 Overvoltage N16],
			[V1 Overvoltage N17],
			[V1 Overvoltage N18],
			[V1 Overvoltage N19],
			[V1 Overvoltage N20],
			[V1 Overvoltage N21],
			[V1 Overvoltage N22],
			[V1 Overvoltage N23],
			[V1 Overvoltage N24],
			[V1 Overvoltage N25],
			[V1 Overvoltage N26],
			[V1 Overvoltage N27],
			[V1 Overvoltage N28],
			[V1 Overvoltage N29],
			[V1 Overvoltage N30],
			[V1 Overvoltage N31],
			[V1 Overvoltage N32],
			[V1 Overvoltage N33],
			[V1 Overvoltage N34],
			[V1 Overvoltage N35],
			[V1 Overvoltage N36],
			[V1 Overvoltage N37],
			[V1 Overvoltage N38],
			[V1 Overvoltage N39],
			[V1 Overvoltage N40],
			[V1 Overvoltage N41],
			[V1 Overvoltage N42],
			[V1 Overvoltage N43],
			[V1 Overvoltage N44],
			[V1 Overvoltage N45],
			[V1 Overvoltage N46],
			[V1 Overvoltage N47],
			[V1 Overvoltage N48],
			[V1 Overvoltage N49],
			[V1 Overvoltage N50],
			[V1 Overvoltage N51],
			[V1 Overvoltage N52],
			[V1 Overvoltage N53],
			[V1 Overvoltage N54],
			[V1 Overvoltage N55],
			[V1 Overvoltage N56],
			[V1 Overvoltage N57],
			[V1 Overvoltage N58],
			[V1 Overvoltage N59],
			[V1 Overvoltage N60],
			[V1 Overvoltage N61],
			[V1 Overvoltage N62],
			[V1 Overvoltage N63],
			[V1 Overvoltage N64],
			[V1 Overvoltage N65],
			[V1 Overvoltage N66],
			[V1 Overvoltage N67],
			[V1 Overvoltage N68],
			[V1 Overvoltage N69],
			[V1 Overvoltage N70],
			[V1 Overvoltage N71],
			[V1 Overvoltage N72],
			[V1 Overvoltage N73],
			[V1 Overvoltage N74],
			[V1 Overvoltage N75],
			[V1 Overvoltage N76],
			[V1 Overvoltage N77],
			[V1 Overvoltage N78],
			[V1 Overvoltage N79],
			[V1 Overvoltage N80],
			[V1 Overvoltage N81],
			[V1 Overvoltage N82],
			[V1 Overvoltage N83],
			[V1 Overvoltage N84],
			[V1 Overvoltage N85],
			[V1 Overvoltage N86],
			[V1 Overvoltage N87],
			[V1 Overvoltage N88],
			[V1 Overvoltage N89],
			[V1 Overvoltage N90],
			[V1 Overvoltage N91],
			[V1 Overvoltage N92],
			[V1 Overvoltage N93],
			[V1 Overvoltage N94],
			[V1 Overvoltage N95],
			[V1 Overvoltage N96],
			[V2 Overvoltage N01],
			[V2 Overvoltage N02],
			[V2 Overvoltage N03],
			[V2 Overvoltage N04],
			[V2 Overvoltage N05],
			[V2 Overvoltage N06],
			[V2 Overvoltage N07],
			[V2 Overvoltage N08],
			[V2 Overvoltage N09],
			[V2 Overvoltage N10],
			[V2 Overvoltage N11],
			[V2 Overvoltage N12],
			[V2 Overvoltage N13],
			[V2 Overvoltage N14],
			[V2 Overvoltage N15],
			[V2 Overvoltage N16],
			[V2 Overvoltage N17],
			[V2 Overvoltage N18],
			[V2 Overvoltage N19],
			[V2 Overvoltage N20],
			[V2 Overvoltage N21],
			[V2 Overvoltage N22],
			[V2 Overvoltage N23],
			[V2 Overvoltage N24],
			[V2 Overvoltage N25],
			[V2 Overvoltage N26],
			[V2 Overvoltage N27],
			[V2 Overvoltage N28],
			[V2 Overvoltage N29],
			[V2 Overvoltage N30],
			[V2 Overvoltage N31],
			[V2 Overvoltage N32],
			[V2 Overvoltage N33],
			[V2 Overvoltage N34],
			[V2 Overvoltage N35],
			[V2 Overvoltage N36],
			[V2 Overvoltage N37],
			[V2 Overvoltage N38],
			[V2 Overvoltage N39],
			[V2 Overvoltage N40],
			[V2 Overvoltage N41],
			[V2 Overvoltage N42],
			[V2 Overvoltage N43],
			[V2 Overvoltage N44],
			[V2 Overvoltage N45],
			[V2 Overvoltage N46],
			[V2 Overvoltage N47],
			[V2 Overvoltage N48],
			[V2 Overvoltage N49],
			[V2 Overvoltage N50],
			[V2 Overvoltage N51],
			[V2 Overvoltage N52],
			[V2 Overvoltage N53],
			[V2 Overvoltage N54],
			[V2 Overvoltage N55],
			[V2 Overvoltage N56],
			[V2 Overvoltage N57],
			[V2 Overvoltage N58],
			[V2 Overvoltage N59],
			[V2 Overvoltage N60],
			[V2 Overvoltage N61],
			[V2 Overvoltage N62],
			[V2 Overvoltage N63],
			[V2 Overvoltage N64],
			[V2 Overvoltage N65],
			[V2 Overvoltage N66],
			[V2 Overvoltage N67],
			[V2 Overvoltage N68],
			[V2 Overvoltage N69],
			[V2 Overvoltage N70],
			[V2 Overvoltage N71],
			[V2 Overvoltage N72],
			[V2 Overvoltage N73],
			[V2 Overvoltage N74],
			[V2 Overvoltage N75],
			[V2 Overvoltage N76],
			[V2 Overvoltage N77],
			[V2 Overvoltage N78],
			[V2 Overvoltage N79],
			[V2 Overvoltage N80],
			[V2 Overvoltage N81],
			[V2 Overvoltage N82],
			[V2 Overvoltage N83],
			[V2 Overvoltage N84],
			[V2 Overvoltage N85],
			[V2 Overvoltage N86],
			[V2 Overvoltage N87],
			[V2 Overvoltage N88],
			[V2 Overvoltage N89],
			[V2 Overvoltage N90],
			[V2 Overvoltage N91],
			[V2 Overvoltage N92],
			[V2 Overvoltage N93],
			[V2 Overvoltage N94],
			[V2 Overvoltage N95],
			[V2 Overvoltage N96],
			[V3 Overvoltage N01],
			[V3 Overvoltage N02],
			[V3 Overvoltage N03],
			[V3 Overvoltage N04],
			[V3 Overvoltage N05],
			[V3 Overvoltage N06],
			[V3 Overvoltage N07],
			[V3 Overvoltage N08],
			[V3 Overvoltage N09],
			[V3 Overvoltage N10],
			[V3 Overvoltage N11],
			[V3 Overvoltage N12],
			[V3 Overvoltage N13],
			[V3 Overvoltage N14],
			[V3 Overvoltage N15],
			[V3 Overvoltage N16],
			[V3 Overvoltage N17],
			[V3 Overvoltage N18],
			[V3 Overvoltage N19],
			[V3 Overvoltage N20],
			[V3 Overvoltage N21],
			[V3 Overvoltage N22],
			[V3 Overvoltage N23],
			[V3 Overvoltage N24],
			[V3 Overvoltage N25],
			[V3 Overvoltage N26],
			[V3 Overvoltage N27],
			[V3 Overvoltage N28],
			[V3 Overvoltage N29],
			[V3 Overvoltage N30],
			[V3 Overvoltage N31],
			[V3 Overvoltage N32],
			[V3 Overvoltage N33],
			[V3 Overvoltage N34],
			[V3 Overvoltage N35],
			[V3 Overvoltage N36],
			[V3 Overvoltage N37],
			[V3 Overvoltage N38],
			[V3 Overvoltage N39],
			[V3 Overvoltage N40],
			[V3 Overvoltage N41],
			[V3 Overvoltage N42],
			[V3 Overvoltage N43],
			[V3 Overvoltage N44],
			[V3 Overvoltage N45],
			[V3 Overvoltage N46],
			[V3 Overvoltage N47],
			[V3 Overvoltage N48],
			[V3 Overvoltage N49],
			[V3 Overvoltage N50],
			[V3 Overvoltage N51],
			[V3 Overvoltage N52],
			[V3 Overvoltage N53],
			[V3 Overvoltage N54],
			[V3 Overvoltage N55],
			[V3 Overvoltage N56],
			[V3 Overvoltage N57],
			[V3 Overvoltage N58],
			[V3 Overvoltage N59],
			[V3 Overvoltage N60],
			[V3 Overvoltage N61],
			[V3 Overvoltage N62],
			[V3 Overvoltage N63],
			[V3 Overvoltage N64],
			[V3 Overvoltage N65],
			[V3 Overvoltage N66],
			[V3 Overvoltage N67],
			[V3 Overvoltage N68],
			[V3 Overvoltage N69],
			[V3 Overvoltage N70],
			[V3 Overvoltage N71],
			[V3 Overvoltage N72],
			[V3 Overvoltage N73],
			[V3 Overvoltage N74],
			[V3 Overvoltage N75],
			[V3 Overvoltage N76],
			[V3 Overvoltage N77],
			[V3 Overvoltage N78],
			[V3 Overvoltage N79],
			[V3 Overvoltage N80],
			[V3 Overvoltage N81],
			[V3 Overvoltage N82],
			[V3 Overvoltage N83],
			[V3 Overvoltage N84],
			[V3 Overvoltage N85],
			[V3 Overvoltage N86],
			[V3 Overvoltage N87],
			[V3 Overvoltage N88],
			[V3 Overvoltage N89],
			[V3 Overvoltage N90],
			[V3 Overvoltage N91],
			[V3 Overvoltage N92],
			[V3 Overvoltage N93],
			[V3 Overvoltage N94],
			[V3 Overvoltage N95],
			[V3 Overvoltage N96]
		)
	) AS pvt
	ORDER BY
		TimestampUtc,
		SourceID
GO

/******************************************************************************
##BD_BEGIN
Return up to four tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. Information about which EN50160 Measurement set each device supports.
 4. EN50160 Flicker data for the devices.

NOTE:  If the @OutputDataOnly parameter is set to 1, this procedure will only
return table 4 listed above.  Also, it will not return any error messages.

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetEN50160FlickerDataION.sql#2 $
******************************************************************************/
ALTER PROCEDURE [dbo].[spDAL_GetEN50160FlickerDataION]
(
	@SourceIDs		NVARCHAR(MAX),
	@StartDatesUtc	NVARCHAR(MAX),
	@EndDatesUtc	NVARCHAR(MAX)
)
AS

-- find information about the sources we have selected
DECLARE @TblSources TABLE
(
	ID							INT PRIMARY KEY NOT NULL,
	MeasurementSetIdentifier	NVARCHAR(100)  -- additional info for EN50160
)

INSERT INTO
	@TblSources
SELECT
	ID,
	UPPER(dbo.udfDAL_DetermineEN50160MeasurementSet(ID))
FROM
	dbo.udfDAL_GetSources(@SourceIDs)

-- define the character that we use to separate the different dates
-- within the lists of start and end dates
DECLARE @DelimiterCharacter NCHAR(1)
SET @DelimiterCharacter = CHAR(6)

-- create a table variable to hold the EN50160 results
DECLARE @EN50160_Flicker TABLE
(
	TimestampUtc		DATETIME,
	SourceID			INT,
    V1FlickerN			FLOAT,
    V1FlickerN1			FLOAT,
    V1FlickerN1Eval		FLOAT,
    V1FlickerNInvalid	FLOAT,
    V2FlickerN			FLOAT,
    V2FlickerN1			FLOAT,
    V2FlickerN1Eval		FLOAT,
    V2FlickerNInvalid	FLOAT,
    V3FlickerN			FLOAT,
    V3FlickerN1			FLOAT,
    V3FlickerN1Eval		FLOAT,
    V3FlickerNInvalid	FLOAT
)
 
-- put the raw data in a form that's easier to query
DECLARE @TblData TABLE
(
	SourceID		INT,
	MeasurementName	NVARCHAR(200),
	TimestampUtc	DATETIME2,
	Value			FLOAT
)

-- insert data for the ION devices into the staging table
INSERT INTO
	@TblData
SELECT
	s.ID AS SourceID,
	q.Name AS MeasurementName,
	dl.TimestampUTC AS TimestampUtc,
	dl.Value
FROM
	dbo.DataLog2 dl
	INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
	INNER JOIN @TblSources s on dl.SourceID = s.ID
WHERE
	s.MeasurementSetIdentifier = N'ION'
	AND q.Name IN
	(
		N'Voltage Flicker Count N on Input V1',
		N'Voltage Flicker Count N1 on Input V1',
		N'Voltage Flicker Count N invalid on Input V1',
		N'Voltage Flicker Count N on Input V2',
		N'Voltage Flicker Count N1 on Input V2',
		N'Voltage Flicker Count N invalid on Input V2',
		N'Voltage Flicker Count N on Input V3',
		N'Voltage Flicker Count N1 on Input V3',
		N'Voltage Flicker Count N invalid on Input V3'
	)
	AND EXISTS
	(
		SELECT
			1
		FROM
			dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
		WHERE
			dl.TimestampUTC > ts.StartDate 
			AND dl.TimestampUTC <= ts.EndDate
	)
ORDER BY
	TimestampUTC,
	SourceID,
	q.Name

-- select data for the ION devices into the results table
SELECT
	TimestampUtc,
	SourceID,
	[Voltage Flicker Count N on Input V1]			AS V1FlickerN,
	[Voltage Flicker Count N1 on Input V1]			AS V1FlickerN1,
	CASE [Voltage Flicker Count N on Input V1]
		WHEN 0 THEN NULL
		ELSE [Voltage Flicker Count N1 on Input V1]/[Voltage Flicker Count N on Input V1]
	END AS V1FlickerN1Eval,
	[Voltage Flicker Count N invalid on Input V1]	AS V1FlickerNInvalid,
	[Voltage Flicker Count N on Input V2]			AS V2FlickerN,
	[Voltage Flicker Count N1 on Input V2]			AS V2FlickerN1,
	CASE [Voltage Flicker Count N on Input V2]
		WHEN 0 THEN NULL
		ELSE [Voltage Flicker Count N1 on Input V2]/[Voltage Flicker Count N on Input V2]
	END AS V2FlickerN1Eval,
	[Voltage Flicker Count N invalid on Input V2]	AS V2FlickerNInvalid,
	[Voltage Flicker Count N on Input V3]			AS V3FlickerN,
	[Voltage Flicker Count N1 on Input V3]			AS V3FlickerN1,
	CASE [Voltage Flicker Count N on Input V3]
		WHEN 0 THEN NULL
		ELSE [Voltage Flicker Count N1 on Input V3]/[Voltage Flicker Count N on Input V3]
	END AS V3FlickerN1Eval,
	[Voltage Flicker Count N invalid on Input V3]	AS V3FlickerNInvalid
FROM
	@TblData
PIVOT
(
	MAX(Value)
	FOR MeasurementName IN
	(
		[Voltage Flicker Count N on Input V1],
		[Voltage Flicker Count N1 on Input V1],
		[Voltage Flicker Count N invalid on Input V1],
		[Voltage Flicker Count N on Input V2],
		[Voltage Flicker Count N1 on Input V2],
		[Voltage Flicker Count N invalid on Input V2],
		[Voltage Flicker Count N on Input V3],
		[Voltage Flicker Count N1 on Input V3],
		[Voltage Flicker Count N invalid on Input V3]
	)
) AS pvt
ORDER BY
	TimestampUtc,
	SourceID

GO

-- update IEC61000_4_30 stored procs

/******************************************************************************
##BD_BEGIN
Return three tables of data:
 1. Time zones in the system that are associated with the devices.
 2. Devices that have been selected.
 3. 4-30 data for the devices.
##BD_END 

$Id: //rdsw/dev/cayman/SW_ION_E/database/Components/DAL/StoredProcedures/01.spDAL_GetIEC61000_4_30Data.sql#5 $
******************************************************************************/

ALTER PROCEDURE dbo.spDAL_GetIEC61000_4_30Data
(
	@SourceIDs			NVARCHAR(MAX),
	@StartDatesUtc		NVARCHAR(MAX),
	@EndDatesUtc		NVARCHAR(MAX),
	@SamplingPeriod		NVARCHAR(256) = '2hr'
)
AS
BEGIN

	-- find information about the sources we have selected
	DECLARE @TblSources TABLE
	(
		ID INT PRIMARY KEY NOT NULL,
		Name NVARCHAR(250) NOT NULL,
		Namespace NVARCHAR(400) NOT NULL,
		Description NVARCHAR(2000),
		TimeZoneID SMALLINT NOT NULL
	)

	INSERT INTO
		@TblSources
	SELECT
		ID,
		Name,
		Namespace,
		Description,
		TimeZoneID
	FROM
		dbo.udfDAL_GetSources(@SourceIDs)

	-- create a temporary table to hold the 4-30 results
	DECLARE @TempResults TABLE
	(
		SourceID INT,
		TimestampUtc DATETIME2,
		V_Unbal_NSC_Mean FLOAT,
		Hz_Mean FLOAT,
		PQ_Evt_Status FLOAT,
		V1_Mean FLOAT,
		V1_Ovrvlt_Mean_pct FLOAT,
		V1_THD_150_180_Mean FLOAT,
		V1_Dip_Mean_pct FLOAT,
		V2_Mean FLOAT,
		V2_Ovrvlt_Mean_pct FLOAT,
		V2_THD_150_180_Mean FLOAT,
		V2_Dip_Mean_pct FLOAT,
		V3_Mean FLOAT,
		V3_Ovrvlt_Mean_pct FLOAT,
		V3_THD_150_180_Mean FLOAT,
		V3_Dip_Mean_pct FLOAT,
		V_Unbal_ZSC_Mean FLOAT,
		VoltageFlkV1 FLOAT,
		VoltageFlkV2 FLOAT,
		VoltageFlkV3 FLOAT
	)

	-- declare a table variable that will allow us to put the raw data into
	-- a format that's easier to perform a pivot on
	DECLARE @TblData TABLE
	(
		SourceID INT,
		MeasurementName NVARCHAR(200),
		TimestampUtc DATETIME2,
		Value FLOAT
	)

	-- define the character that we use to separate the different dates
	-- within the lists of start and end dates
	DECLARE @DelimiterCharacter NCHAR(1)
	SET @DelimiterCharacter = CHAR(6)

	-- determine what data to return depending on the sampling period
	DECLARE @ValidSamplingPeriod BIT
	SET @ValidSamplingPeriod = 1  -- Assume that the sampling period is one that we expect to receive
	DECLARE @SamplingPeriodUpperBoundary INT

	IF (@SamplingPeriod = '2hr')  
	BEGIN
		-- Insert the raw data into our "staging" table.
		INSERT INTO
			@TblData
		SELECT
			s.ID AS SourceID,
			q.Name AS MeasurementName,
			dl.TimestampUTC,
			dl.Value
		FROM
			dbo.DataLog2 dl
			INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
			INNER JOIN @TblSources s on dl.SourceID = s.ID			
		WHERE
			(q.Name IN 
				(
					'Voltage Flicker Plt on Input V1'
					,'Voltage Flicker Plt on Input V2'
					,'Voltage Flicker Plt on Input V3'
					,'Power Quality Event 2-hour Status'
					,'Voltage Magnitude 2-hour Mean on Input V1'
					,'Voltage Magnitude 2-hour Mean on Input V2'
					,'Voltage Magnitude 2-hour Mean on Input V3'
					,'Voltage Total Harmonic Distortion 2-hour Mean on Input V1'
					,'Voltage Total Harmonic Distortion 2-hour Mean on Input V2'
					,'Voltage Total Harmonic Distortion 2-hour Mean on Input V3'
					,'Voltage Dip 2-hour Mean as Percentage of Nominal on Input V1'
					,'Overvoltage 2-hour Mean as Percentage of Nominal on Input V1'
					,'Voltage Dip 2-hour Mean as Percentage of Nominal on Input V2'
					,'Overvoltage 2-hour Mean as Percentage of Nominal on Input V2'
					,'Voltage Dip 2-hour Mean as Percentage of Nominal on Input V3'
					,'Overvoltage 2-hour Mean as Percentage of Nominal on Input V3'
					,'Voltage Unbalance Negative Sequence Component 2-hour Mean'
					,'Voltage Unbalance Zero Sequence Component 2-hour Mean'
					,'Power Frequency 2-hour Mean'
				)
			)
			AND EXISTS
			(
				SELECT
					1
				FROM
					dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
				WHERE
					dl.TimestampUTC >= ts.StartDate 
					AND dl.TimestampUTC <= ts.EndDate
			)
		ORDER BY
			SourceID,
			TimestampUTC,
			QuantityID

		-- Insert data into the results table.
		INSERT INTO
			@TempResults
		SELECT
			SourceID
			,TimestampUtc
			,[Voltage Unbalance Negative Sequence Component 2-hour Mean] AS V_Unbal_NSC_Mean
			,[Power Frequency 2-hour Mean] AS Hz_Mean
			,[Power Quality Event 2-hour Status] AS PQ_Evt_Status
			,[Voltage Magnitude 2-hour Mean on Input V1] AS V1_Mean
			,[Overvoltage 2-hour Mean as Percentage of Nominal on Input V1] AS V1_Ovrvlt_Mean_pct
			,[Voltage Total Harmonic Distortion 2-hour Mean on Input V1] AS V1_THD_150_180_Mean
			,[Voltage Dip 2-hour Mean as Percentage of Nominal on Input V1] AS V1_Dip_Mean_pct
			,[Voltage Magnitude 2-hour Mean on Input V2] AS V2_Mean
			,[Overvoltage 2-hour Mean as Percentage of Nominal on Input V2] AS V2_Ovrvlt_Mean_pct
			,[Voltage Total Harmonic Distortion 2-hour Mean on Input V2] AS V2_THD_150_180_Mean
			,[Voltage Dip 2-hour Mean as Percentage of Nominal on Input V2] AS V2_Dip_Mean_pct
			,[Voltage Magnitude 2-hour Mean on Input V3] AS V3_Mean
			,[Overvoltage 2-hour Mean as Percentage of Nominal on Input V3] AS V3_Ovrvlt_Mean_pct
			,[Voltage Total Harmonic Distortion 2-hour Mean on Input V3] AS V3_THD_150_180_Mean
			,[Voltage Dip 2-hour Mean as Percentage of Nominal on Input V3] AS V3_Dip_Mean_pct
			,[Voltage Unbalance Zero Sequence Component 2-hour Mean] AS V_Unbal_ZSC_Mean
			,NULL AS VoltageFlkV1
			,NULL AS VoltageFlkV2
			,NULL AS VoltageFlkV3
		FROM 
		(
			SELECT
				SourceID,
				TimestampUtc,
				MeasurementName,
				Value
			FROM
				@TblData
			WHERE
				MeasurementName IN 
				(
					'Power Quality Event 2-hour Status'
					,'Voltage Magnitude 2-hour Mean on Input V1'
					,'Voltage Magnitude 2-hour Mean on Input V2'
					,'Voltage Magnitude 2-hour Mean on Input V3'
					,'Voltage Total Harmonic Distortion 2-hour Mean on Input V1'
					,'Voltage Total Harmonic Distortion 2-hour Mean on Input V2'
					,'Voltage Total Harmonic Distortion 2-hour Mean on Input V3'
					,'Voltage Dip 2-hour Mean as Percentage of Nominal on Input V1'
					,'Overvoltage 2-hour Mean as Percentage of Nominal on Input V1'
					,'Voltage Dip 2-hour Mean as Percentage of Nominal on Input V2'
					,'Overvoltage 2-hour Mean as Percentage of Nominal on Input V2'
					,'Voltage Dip 2-hour Mean as Percentage of Nominal on Input V3'
					,'Overvoltage 2-hour Mean as Percentage of Nominal on Input V3'
					,'Voltage Unbalance Negative Sequence Component 2-hour Mean'
					,'Voltage Unbalance Zero Sequence Component 2-hour Mean'
					,'Power Frequency 2-hour Mean'
				)
		) p
		PIVOT
		(
			AVG(Value)
			FOR MeasurementName IN 
				(
					[Power Quality Event 2-hour Status]
					,[Voltage Magnitude 2-hour Mean on Input V1]
					,[Voltage Magnitude 2-hour Mean on Input V2]
					,[Voltage Magnitude 2-hour Mean on Input V3]
					,[Voltage Total Harmonic Distortion 2-hour Mean on Input V1]
					,[Voltage Total Harmonic Distortion 2-hour Mean on Input V2]
					,[Voltage Total Harmonic Distortion 2-hour Mean on Input V3]
					,[Voltage Dip 2-hour Mean as Percentage of Nominal on Input V1]
					,[Overvoltage 2-hour Mean as Percentage of Nominal on Input V1]
					,[Voltage Dip 2-hour Mean as Percentage of Nominal on Input V2]
					,[Overvoltage 2-hour Mean as Percentage of Nominal on Input V2]
					,[Voltage Dip 2-hour Mean as Percentage of Nominal on Input V3]
					,[Overvoltage 2-hour Mean as Percentage of Nominal on Input V3]
					,[Voltage Unbalance Negative Sequence Component 2-hour Mean]
					,[Voltage Unbalance Zero Sequence Component 2-hour Mean]
					,[Power Frequency 2-hour Mean]
				)
		) AS pvt
		ORDER BY
			SourceID ASC,
			TimestampUtc ASC

		--Update Flicker Values
		--The following update statements update the pivot'd result set with flicker
		--values for all phases. Other 4-30 readings are 2 hour mean readings. The meter
		--takes care of providing 1 reading for a 2 hour period. For flicker values
		--readings are submitted every 10 minutes with the same value for the sampling
		--period. Example 8 readings for a 2 hour sampling period all have the same value.
		--A conventional join on pivot'd 4-30 data to Flicker data is not possible as the
		--other data has been observed to arrive up to 5 seconds late. 
		--Upper Boundary = TimestampUtc + 119 minutes. 2 hours - 1 minute. Ensures, when 
		--the record arrives late that flicker data from the next interval is not picked.
		--Lower Boundary = TimestampUtc - 1 minute. Ensures, when the record arrives late
		--that flicker data for that interval is not ignored.
		SET @SamplingPeriodUpperBoundary = 119

		UPDATE
			@TempResults
		SET
			VoltageFlkV1 = d.Value 
		FROM
			@TempResults tr
			INNER JOIN @TblData d ON
				d.SourceID = tr.SourceID 
				AND d.MeasurementName = 'Voltage Flicker Plt on Input V1'
				AND d.TimestampUtc >= DATEADD(mi, -1, tr.TimestampUtc)
				AND d.TimestampUtc < DateAdd(mi,@SamplingPeriodUpperBoundary,tr.TimestampUtc)

		UPDATE
			@TempResults
		SET
			VoltageFlkV2 = d.Value 
		FROM
			@TempResults tr
			INNER JOIN @TblData d ON
				d.SourceID = tr.SourceID 
				AND d.MeasurementName = 'Voltage Flicker Plt on Input V2'
				AND d.TimestampUtc >= DATEADD(mi, -1, tr.TimestampUtc)
				AND d.TimestampUtc < DATEADD(mi,@SamplingPeriodUpperBoundary,tr.TimestampUtc)

		UPDATE
			@TempResults
		SET
			VoltageFlkV3 = d.Value 
		FROM
			@TempResults tr
			INNER JOIN @TblData d ON
				d.SourceID = tr.SourceID 
				AND d.MeasurementName = 'Voltage Flicker Plt on Input V3'
				AND d.TimestampUtc >= DATEADD(mi, -1, tr.TimestampUtc)
				AND d.TimestampUtc < DateAdd(mi,@SamplingPeriodUpperBoundary,tr.TimestampUtc)
	END

	ELSE IF (@SamplingPeriod = '10min')
	BEGIN
		-- Insert the raw data into our "staging" table.
		INSERT INTO
			@TblData
		SELECT
			s.ID AS SourceID,
			q.Name AS MeasurementName,
			dl.TimestampUTC,
			dl.Value
		FROM
			dbo.DataLog2 dl
			INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
			INNER JOIN @TblSources s on dl.SourceID = s.ID
		WHERE
			(q.Name IN 
				(
					'Voltage Flicker Pst on Input V2'
					,'Voltage Flicker Pst on Input V3'
					,'Voltage Flicker Pst on Input V1'
					,'Power Quality Event 10-minute Status'
					,'Voltage Magnitude 10-minute Mean on Input V1'
					,'Voltage Magnitude 10-minute Mean on Input V2'
					,'Voltage Magnitude 10-minute Mean on Input V3'
					,'Voltage Total Harmonic Distortion 10-minute Mean on Input V1'
					,'Voltage Total Harmonic Distortion 10-minute Mean on Input V2'
					,'Voltage Total Harmonic Distortion 10-minute Mean on Input V3'
					,'Voltage Dip 10-minute Mean as Percentage of Nominal on Input V1'
					,'Overvoltage 10-minute Mean as Percentage of Nominal on Input V1'
					,'Voltage Dip 10-minute Mean as Percentage of Nominal on Input V2'
					,'Overvoltage 10-minute Mean as Percentage of Nominal on Input V2'
					,'Voltage Dip 10-minute Mean as Percentage of Nominal on Input V3'
					,'Overvoltage 10-minute Mean as Percentage of Nominal on Input V3'
					,'Voltage Unbalance Negative Sequence Component 10-minute Mean'
					,'Voltage Unbalance Zero Sequence Component 10-minute Mean'
					,'Power Frequency 10-minute Mean'
				)
			)
			AND EXISTS
			(
				SELECT
					1
				FROM
					dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
				WHERE
					dl.TimestampUTC >= ts.StartDate 
					AND dl.TimestampUTC <= ts.EndDate
			)
		ORDER BY
			SourceID,
			TimestampUTC,
			QuantityID

		-- Insert data into the results table.
		INSERT INTO
			@TempResults
		SELECT
			SourceID
			,TimestampUtc
			,[Voltage Unbalance Negative Sequence Component 10-minute Mean] AS V_Unbal_NSC_Mean
			,[Power Frequency 10-minute Mean] AS V_Unbal_ZSC_Mean
			,[Power Quality Event 10-minute Status] AS PQ_Evt_Status
			,[Voltage Magnitude 10-minute Mean on Input V1] AS V1_Mean
			,[Overvoltage 10-minute Mean as Percentage of Nominal on Input V1] AS V1_Ovrvlt_Mean_pct
			,[Voltage Total Harmonic Distortion 10-minute Mean on Input V1] AS V1_THD_150_180_Mean
			,[Voltage Dip 10-minute Mean as Percentage of Nominal on Input V1] AS V1_Dip_Mean_pct
			,[Voltage Magnitude 10-minute Mean on Input V2] AS V2_Mean
			,[Overvoltage 10-minute Mean as Percentage of Nominal on Input V2] AS V2_Ovrvlt_Mean_pct
			,[Voltage Total Harmonic Distortion 10-minute Mean on Input V2] AS V2_THD_150_180_Mean
			,[Voltage Dip 10-minute Mean as Percentage of Nominal on Input V2] AS V2_Dip_Mean_pct
			,[Voltage Magnitude 10-minute Mean on Input V3] AS V3_Mean
			,[Overvoltage 10-minute Mean as Percentage of Nominal on Input V3] AS V3_Ovrvlt_Mean_pct
			,[Voltage Total Harmonic Distortion 10-minute Mean on Input V3] AS V3_THD_150_180_Mean
			,[Voltage Dip 10-minute Mean as Percentage of Nominal on Input V3] AS V3_Dip_Mean_pct
			,[Voltage Unbalance Zero Sequence Component 10-minute Mean] AS Hz_Mean
			,NULL AS VoltageFlkV1
			,NULL AS VoltageFlkV2
			,NULL AS VoltageFlkV3
		FROM
		(
			SELECT
				SourceID,
				TimestampUtc,
				MeasurementName,
				Value
			FROM
				@TblData
			WHERE
				MeasurementName IN 
					(
						'Power Quality Event 10-minute Status'
						,'Voltage Magnitude 10-minute Mean on Input V1'
						,'Voltage Magnitude 10-minute Mean on Input V2'
						,'Voltage Magnitude 10-minute Mean on Input V3'
						,'Voltage Total Harmonic Distortion 10-minute Mean on Input V1'
						,'Voltage Total Harmonic Distortion 10-minute Mean on Input V2'
						,'Voltage Total Harmonic Distortion 10-minute Mean on Input V3'
						,'Voltage Dip 10-minute Mean as Percentage of Nominal on Input V1'
						,'Overvoltage 10-minute Mean as Percentage of Nominal on Input V1'
						,'Voltage Dip 10-minute Mean as Percentage of Nominal on Input V2'
						,'Overvoltage 10-minute Mean as Percentage of Nominal on Input V2'
						,'Voltage Dip 10-minute Mean as Percentage of Nominal on Input V3'
						,'Overvoltage 10-minute Mean as Percentage of Nominal on Input V3'
						,'Voltage Unbalance Negative Sequence Component 10-minute Mean'
						,'Voltage Unbalance Zero Sequence Component 10-minute Mean'
						,'Power Frequency 10-minute Mean'
					)
		) p
		PIVOT
		(
			AVG(Value)
			FOR MeasurementName IN 
			(
				[Power Quality Event 10-minute Status]
				,[Voltage Magnitude 10-minute Mean on Input V1]
				,[Voltage Magnitude 10-minute Mean on Input V2]
				,[Voltage Magnitude 10-minute Mean on Input V3]
				,[Voltage Total Harmonic Distortion 10-minute Mean on Input V1]
				,[Voltage Total Harmonic Distortion 10-minute Mean on Input V2]
				,[Voltage Total Harmonic Distortion 10-minute Mean on Input V3]
				,[Voltage Dip 10-minute Mean as Percentage of Nominal on Input V1]
				,[Overvoltage 10-minute Mean as Percentage of Nominal on Input V1]
				,[Voltage Dip 10-minute Mean as Percentage of Nominal on Input V2]
				,[Overvoltage 10-minute Mean as Percentage of Nominal on Input V2]
				,[Voltage Dip 10-minute Mean as Percentage of Nominal on Input V3]
				,[Overvoltage 10-minute Mean as Percentage of Nominal on Input V3]
				,[Voltage Unbalance Negative Sequence Component 10-minute Mean]
				,[Voltage Unbalance Zero Sequence Component 10-minute Mean]
				,[Power Frequency 10-minute Mean]
			)
		) AS pvt
		ORDER BY
			SourceID ASC,
			TimestampUtc ASC

		--Update Flicker Values
		--The following update statements update the pivot'd result set with flicker
		--values for all phases. Other 4-30 readings are 10 minute mean readings. The meter
		--takes care of providing 1 reading for a 10 minute period. For flicker values,
		--readings are submitted every 10 minutes with the same value for the sampling
		--period. For a 10 minute sampling period, only one reaidng is submitted.
		--A conventional join on pivot'd 4-30 data to Flicker data is not possible as the
		--other data has been observed to arrive up to 5 seconds late.
		--Upper Boundary = TimestampUtc + 9 minutes. Ensures, when the record arrives late
		--that flicker data from the next interval is not picked.
		--Lower Boundary = TimestampUtc - 1 minute. Ensures, when the record arrives late
		--that flicker data for that interval is not ignored.
		SET @SamplingPeriodUpperBoundary = 9  

		UPDATE
			@TempResults
		SET
			VoltageFlkV1 = d.Value 
		FROM
			@TempResults tr
			INNER JOIN @TblData d ON
				d.SourceID = tr.SourceID 
				AND d.MeasurementName = 'Voltage Flicker Pst on Input V1'
				AND d.TimestampUtc >= DATEADD(mi, -1, tr.TimestampUtc)
				AND d.TimestampUtc < DateAdd(mi,@SamplingPeriodUpperBoundary,tr.TimestampUtc)
		
		UPDATE
			@TempResults
		SET
			VoltageFlkV2 = d.Value 
		FROM
			@TempResults tr
			INNER JOIN @TblData d ON
				d.SourceID = tr.SourceID 
				AND d.MeasurementName = 'Voltage Flicker Pst on Input V2'
				AND d.TimestampUtc >= DATEADD(mi, -1, tr.TimestampUtc)
				AND d.TimestampUtc < DateAdd(mi,@SamplingPeriodUpperBoundary,tr.TimestampUtc)

		UPDATE
			@TempResults
		SET
			VoltageFlkV3 = d.Value 
		FROM
			@TempResults tr
			INNER JOIN @TblData d ON
				d.SourceID = tr.SourceID 
				AND d.MeasurementName = 'Voltage Flicker Pst on Input V3'
				AND d.TimestampUtc >= DATEADD(mi, -1, tr.TimestampUtc)
				AND d.TimestampUtc < DateAdd(mi,@SamplingPeriodUpperBoundary,tr.TimestampUtc)
	END
	ELSE IF (@SamplingPeriod = '3sec')
	BEGIN
		-- NOTE: Frequency and Flicker data is not available for the 3 second sampling
		-- period. The columns are always empty when 3sec is specified.
		-- Insert the raw data into our "staging" table.
		INSERT INTO
			@TblData
		SELECT
			s.ID AS SourceID,
			q.Name AS MeasurementName,
			dl.TimestampUTC,
			dl.Value
		FROM
			dbo.DataLog2 dl
			INNER JOIN dbo.Quantity q on dl.QuantityID = q.ID
			INNER JOIN @TblSources s on dl.SourceID = s.ID
		WHERE
			(q.Name IN
				(
					'Power Quality Event 150/180-cycle Status'
					,'Voltage Magnitude 150/180-cycle Mean on Input V1'
					,'Voltage Magnitude 150/180-cycle Mean on Input V2'
					,'Voltage Magnitude 150/180-cycle Mean on Input V3'
					,'Voltage Total Harmonic Distortion 150/180-cycle Mean on Input V1'
					,'Voltage Total Harmonic Distortion 150/180-cycle Mean on Input V2'
					,'Voltage Total Harmonic Distortion 150/180-cycle Mean on Input V3'
					,'Voltage Dip 150/180-cycle Mean as Percentage of Nominal on Input V1'
					,'Overvoltage 150/180-cycle Mean as Percentage of Nominal on Input V1'
					,'Voltage Dip 150/180-cycle Mean as Percentage of Nominal on Input V2'
					,'Overvoltage 150/180-cycle Mean as Percentage of Nominal on Input V2'
					,'Voltage Dip 150/180-cycle Mean as Percentage of Nominal on Input V3'
					,'Overvoltage 150/180-cycle Mean as Percentage of Nominal on Input V3'
					,'Voltage Unbalance Negative Sequence Component 150/180-cycle Mean'
					,'Voltage Unbalance Zero Sequence Component 150/180-cycle Mean'
				)
			)
			AND EXISTS
			(
				SELECT
					1
				FROM
					dbo.udf_SplitDateListsToTable(@StartDatesUtc, @EndDatesUtc, @DelimiterCharacter) ts
				WHERE
					dl.TimestampUTC >= ts.StartDate 
					AND dl.TimestampUTC <= ts.EndDate
			)
		ORDER BY
			SourceID,
			TimestampUTC,
			QuantityID

		-- Insert data into the results table.
		INSERT INTO
			@TempResults
		SELECT
			SourceID
			,TimestampUtc
			,[Voltage Unbalance Negative Sequence Component 150/180-cycle Mean] AS V_Unbal_NSC_Mean 
			,NULL AS Hz_Mean
			,[Power Quality Event 150/180-cycle Status] AS PQ_Evt_Status
			,[Voltage Magnitude 150/180-cycle Mean on Input V1] AS V1_Mean
			,[Overvoltage 150/180-cycle Mean as Percentage of Nominal on Input V1] AS V1_Ovrvlt_Mean_pct
			,[Voltage Total Harmonic Distortion 150/180-cycle Mean on Input V1] AS V1_THD_150_180_Mean
			,[Voltage Dip 150/180-cycle Mean as Percentage of Nominal on Input V1] AS V1_Dip_Mean_pct
			,[Voltage Magnitude 150/180-cycle Mean on Input V2] AS V2_Mean
			,[Overvoltage 150/180-cycle Mean as Percentage of Nominal on Input V2] AS V2_Ovrvlt_Mean_pct
			,[Voltage Total Harmonic Distortion 150/180-cycle Mean on Input V2] AS V2_THD_150_180_Mean
			,[Voltage Dip 150/180-cycle Mean as Percentage of Nominal on Input V2] AS V2_Dip_Mean_pct
			,[Voltage Magnitude 150/180-cycle Mean on Input V3] AS V3_Mean
			,[Overvoltage 150/180-cycle Mean as Percentage of Nominal on Input V3] AS V3_Ovrvlt_Mean_pct
			,[Voltage Total Harmonic Distortion 150/180-cycle Mean on Input V3] AS V3_THD_150_180_Mean
			,[Voltage Dip 150/180-cycle Mean as Percentage of Nominal on Input V3] AS V3_Dip_Mean_pct
			,[Voltage Unbalance Zero Sequence Component 150/180-cycle Mean] AS V_Unbal_ZSC_Mean
			,NULL AS VoltageFlkV1
			,NULL AS VoltageFlkV2
			,NULL AS VoltageFlkV3
		FROM
		(
			SELECT
				SourceID,
				TimestampUtc,
				MeasurementName,
				Value
			FROM
				@TblData
			WHERE
				MeasurementName IN 
				(
					'Power Quality Event 150/180-cycle Status'
					,'Voltage Magnitude 150/180-cycle Mean on Input V1'
					,'Voltage Magnitude 150/180-cycle Mean on Input V2'
					,'Voltage Magnitude 150/180-cycle Mean on Input V3'
					,'Voltage Total Harmonic Distortion 150/180-cycle Mean on Input V1'
					,'Voltage Total Harmonic Distortion 150/180-cycle Mean on Input V2'
					,'Voltage Total Harmonic Distortion 150/180-cycle Mean on Input V3'
					,'Voltage Dip 150/180-cycle Mean as Percentage of Nominal on Input V1'
					,'Overvoltage 150/180-cycle Mean as Percentage of Nominal on Input V1'
					,'Voltage Dip 150/180-cycle Mean as Percentage of Nominal on Input V2'
					,'Overvoltage 150/180-cycle Mean as Percentage of Nominal on Input V2'
					,'Voltage Dip 150/180-cycle Mean as Percentage of Nominal on Input V3'
					,'Overvoltage 150/180-cycle Mean as Percentage of Nominal on Input V3'
					,'Voltage Unbalance Negative Sequence Component 150/180-cycle Mean'
					,'Voltage Unbalance Zero Sequence Component 150/180-cycle Mean'
				)
		) p
		PIVOT
		(
			AVG(Value)
			FOR MeasurementName IN 
			(
				[Power Quality Event 150/180-cycle Status]
				,[Voltage Magnitude 150/180-cycle Mean on Input V1]
				,[Voltage Magnitude 150/180-cycle Mean on Input V2]
				,[Voltage Magnitude 150/180-cycle Mean on Input V3]
				,[Voltage Total Harmonic Distortion 150/180-cycle Mean on Input V1]
				,[Voltage Total Harmonic Distortion 150/180-cycle Mean on Input V2]
				,[Voltage Total Harmonic Distortion 150/180-cycle Mean on Input V3]
				,[Voltage Dip 150/180-cycle Mean as Percentage of Nominal on Input V1]
				,[Overvoltage 150/180-cycle Mean as Percentage of Nominal on Input V1]
				,[Voltage Dip 150/180-cycle Mean as Percentage of Nominal on Input V2]
				,[Overvoltage 150/180-cycle Mean as Percentage of Nominal on Input V2]
				,[Voltage Dip 150/180-cycle Mean as Percentage of Nominal on Input V3]
				,[Overvoltage 150/180-cycle Mean as Percentage of Nominal on Input V3]
				,[Voltage Unbalance Negative Sequence Component 150/180-cycle Mean]
				,[Voltage Unbalance Zero Sequence Component 150/180-cycle Mean]
			)
		) AS pvt
		ORDER BY
			SourceID ASC,
			TimestampUtc ASC
	END
	ELSE
	BEGIN
		-- The sampling period is invalid; raise error
		RAISERROR ( 'Invalid 4-30 sampling period provided: ''2hr'', ''10min'' or ''3sec'' supported', 16, 1 )
	END
	-- return the 4-30 data for the sources we have selected
	SELECT
		SourceID,
		TimestampUtc,
		V_Unbal_NSC_Mean,
		Hz_Mean,
		PQ_Evt_Status,
		V1_Mean,
		V1_Ovrvlt_Mean_pct,
		V1_THD_150_180_Mean,
		V1_Dip_Mean_pct,
		V2_Mean,
		V2_Ovrvlt_Mean_pct,
		V2_THD_150_180_Mean,
		V2_Dip_Mean_pct,
		V3_Mean,
		V3_Ovrvlt_Mean_pct,
		V3_THD_150_180_Mean,
		V3_Dip_Mean_pct,
		V_Unbal_ZSC_Mean,
		VoltageFlkV1,
		VoltageFlkV2,
		VoltageFlkV3
	FROM
		@TempResults
END
GO
	SELECT dbo.FormatStatusMessage('Step 10: Completed.')