我尝试在SAME表上使用嵌套的SELECT进行SQL查询(表中的UID是多态的)。
问题是我的嵌套SELECT总是返回NULL。
以下是查询:
SELECT
Ent.UID,
Measurement.MeasurementClass AS Type,
Substation.TG8000_Name AS Station,
SUBSTRING(Measurement.TG8000_Name,1,5) AS Travee,
SUBSTRING(Measurement.TG8000_Name,6,8) AS Equipement,
Measurement.ClonedFromMeasurement,
(SELECT TOP 1 TG8000_Name
FROM [BD_Sonel_PTG].[dbo].[Measurement]
WHERE (UID=Measurement.ClonedFromMeasurement) ) AS Template,
LongName.LongName AS Description,
Measurement.MeasurementUnit AS Units,
MeasurementLimit.LowLimit,
MeasurementLimit.HighLimit,
RTU.TG8000_Name AS RTUName,
RTU.RTUProtocol,
RTU.Configuration AS RTUConfiguration,
Telemetry.Address,
Measurement.DisplayName AS Display,
REPLACE(Measurement.MeasurementClass,' ','')+':'+REPLACE(Substation.TG8000_Name,' ','')+'.'+REPLACE(Measurement.TG8000_Name,' ','') AS Tren
FROM EntityName Ent
LEFT JOIN LongName ON Ent.UID = Longname.UID /* chaque point possède une description pour une langue donnée */
LEFT JOIN Measurement ON Ent.UID = Measurement.UID /* chaque point de type measurement possède des infos suppl dansla table Measurement */
LEFT JOIN Substation ON Measurement.Substation = Substation.UID /* chaque point est lié à l'UID d'une station (Table Substations) */
LEFT JOIN Telemetry ON Telemetry.DataSourceFor = Ent.UID /* 1 point télémétré est lié à une entrée de Telemetry (DataSourceFor) */
LEFT JOIN MeasurementLimit ON Ent.UID = MeasurementLimit.UID /* Chaque point de type measurement est lié à des paramètres limites */
LEFT JOIN RTU ON Telemetry.RTU = RTU.UID /* chaque point télémétré est associé à un l'UID d'un RTU dont les paramètres sont dans la table RTU */
/* LEFT JOIN EntityName Ent2 ON Ent.UID = Measurement.ClonedFromMeasurement*/
WHERE Ent.EntityType = 'Measurement'
ORDER BY Substation.TG8000_Name, Measurement.TG8000_Name;
GO
当我使用硬编码值9616执行嵌套选择时,它可以工作!
... (SELECT TOP 1 TG8000_Name
FROM [BD_Sonel_PTG].[dbo].[Measurement]
WHERE (UID='9616') ) AS Template,
...
所以我想这是对Measurement.ClonedFromMeasurement的访问不起作用......
但正如您在查询中看到的那样,我还输出了Measurement.ClonedFromMeasurement的值以查看其中的内容,并且我正确地接收了我的9616值(从一行到另一行)。
如何在嵌套查询中访问Measurement.ClonedFromMeasurement?
答案 0 :(得分:0)
尝试为此
进行自我加入(SELECT TOP 1 TG8000_Name
FROM [BD_Sonel_PTG].[dbo].[Measurement] m1,m2 on
m1.UID = m2.ClonedFromMeasurement) AS Template
OR
(SELECT TOP 1 TG8000_Name
FROM [BD_Sonel_PTG].[dbo].[Measurement]
where UID in (select distinct ClonedFromMeasurement from
[BD_Sonel_PTG].[dbo].[Measurement]) AS Template
答案 1 :(得分:0)
“UID”是INT列吗?如果是这样,请尝试使用
UID=Convert(int,Measurement.ClonedFromMeasurement)
。