SELECT 
(
SELECT 
	Meter.AssetKey as [@id],
	(	
		SELECT 
			m.AssetKey as name,
			Circuit.Name as circuit,
			dbo.GetJSONValueForProperty(ExtraData, 'xmlConfigLastRevisedDate') as xmlConfigLastRevisedDate,
			dbo.GetJSONValueForProperty(ExtraData, 'xmlConfigTopologyFile') as xmlConfigTopologyFile,
			dbo.GetJSONValueForProperty(ExtraData, 'xmlConfigAfcFile') as xmlConfigAfcFile,
			dbo.GetJSONValueForProperty(ExtraData, 'relayPurpose') as relayPurpose,
			dbo.GetJSONValueForProperty(ExtraData, 'deviceControlled') as deviceControlled,
			dbo.GetJSONValueForProperty(ExtraData, 'normalState') as normalState,
			SubStation.Name as subStation,
			dbo.GetJSONValueForProperty(ExtraData, 'sourcePreferred') as sourcePreferred,
			dbo.GetJSONValueForProperty(ExtraData, 'sourceAlternate') as sourceAlternate,
			(SELECT AssetKey FROM Meter p WHERE m.ID = p.ID ) as parentNomral,
			(SELECT AssetKey FROM Meter p WHERE m.ID = p.ID ) as parentAlternate,
			m.Phasing as phaseLabels,
			m.Orientation as orientation,
			dbo.GetJSONValueForProperty(ExtraData, 'rootPngFolder') as rootPngFolder,
			dbo.GetJSONValueForProperty(ExtraData, 'analysisLink') as analysisLink,
			dbo.GetJSONValueForProperty(ExtraData, 'classifyLink') as classifyLink,
			dbo.GetJSONValueForProperty(ExtraData, 'pngLink') as pngLink,
			dbo.GetJSONValueForProperty(ExtraData, 'uncLink') as uncLink,
			m.Make as make,
			m.Model as model,
			meterlocation.AssetKey as stationID,
			meterlocation.Name as stationName,
			CAST(meterlocation.Latitude as nvarchar(max)) as stationLatitude,
			CAST(meterlocation.Longitude as nvarchar(max)) as stationLongitude

		FROM Meter m JOIN
			Circuit on m.CircuitID = Circuit.ID JOIN
			SubStation ON SubStation.ID = m.SubStationID JOIN
			MeterLocation ON M.MeterLocationID = MeterLocation.ID
		WHERE m.ID = Meter.ID
		FOR XML PATH('attributes'), TYPE
	) ,
		(	
		SELECT 
			(
				SELECT 
					Line.AssetKey as [@id],
					MeterLine.LineName as name,
					CAST(Line.VoltageKV as nvarchar(10)) as voltage,
					CAST(Line.AFCLG as nvarchar(max)) as AFCLG,
					CAST(Line.AFCLL as nvarchar(max)) as AFCLL,
					CAST(Line.AFCLLL as nvarchar(max)) as AFCLLL,
					CAST(Line.ThermalRating as nvarchar(max)) as rating50F,
					CAST(Line.Length as nvarchar(max)) as length,
					MeterLocation.AssetKey as endStationID,
					MeterLocation.Name as endStationName,
					(SELECT 
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%VA%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'AN') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Voltage') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as VA,
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%VB%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'BN') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Voltage') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as VB, 
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%VC%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'CN') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Voltage') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as VC, 
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%VX1%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'General1') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Voltage') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as VX1, 
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%VX2%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'General2') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Voltage') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as VX2,
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%VX3%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'General3') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Voltage') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as VX3,
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%VY1%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'General1') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Voltage') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as VY1,
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%VY2%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'General2') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Voltage') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as VY2, 
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%VY3%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'General3') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Voltage') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as VY3, 
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%IA%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'AN') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Current') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as IA, 
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%IB%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'BN') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Current') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as IB, 
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%IC%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'CN') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Current') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as IC, 
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%I1%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'General1') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Current') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as I1, 
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%I2%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'General2') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Current') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as I2, 
						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%I3%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'General3') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Current') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as I3, 
 						( SELECT TOP 1 Series.SourceIndexes From Channel JOIN Series ON Channel.MeterID = Meter.ID AND Channel.LineID = Line.ID AND Channel.ID = Series.ChannelID AND Channel.Name LIKE '%IN%' AND Channel.PhaseID = (SELECT ID FROM Phase WHERE Name = 'IN') AND Channel.MeasurementTypeID = (SELECT ID FROM MeasurementType WHERE Name = 'Current') AND Channel.MeasurementCharacteristicID = (SELECT ID FROM MeasurementCharacteristic WHERE Name = 'Instantaneous')) as [IN]

					 FOR XML PATH ('channels'), TYPE
					 )
				FROM Line JOIN
				     MeterLine ON Line.ID = MeterLine.LineID AND MeterLine.MeterID = Meter.ID JOIN
					 MeterLocationLine ON Line.ID = MeterLocationLine.LineID AND MeterLocationLine.MeterLocationID != Meter.MeterLocationID JOIN
					 MeterLocation ON MeterLocation.ID = MeterLocationLine.MeterLocationID
			 FOR XML PATH ('line'),TYPE
			)
		FOR XML PATH('lines'), TYPE
	) 

FROM 
		Meter
FOR XML PATH('device'), TYPE
) FOR XML PATH('openFLE'), TYPE