我有以下SELECT
声明。
SELECT t.ID,
t.SiteID,
t.SpareID,
t.SpareLocationID,
t.Qty,
t.IsDefault
FROM TrainingDB.dbo.LocationsPerSpare t
WHERE t.SpareID IN
(SELECT s.SpareID
FROM TrainingDB.dbo.LocationsPerSpare s
WHERE s.SpareLocationID = t.SpareLocationID
AND s.SpareID = t.SpareID
GROUP BY s.SpareID
HAVING COUNT(CONVERT(VARCHAR(36), s.SpareID)) > 2)
ORDER BY t.SpareID
如果我执行该脚本,它会返回NULL
. 但是,如果我如下删除t.
别名,它会正常运行。
SELECT ID,
SiteID,
SpareID,
SpareLocationID,
Qty,
IsDefault
FROM TrainingDB.dbo.LocationsPerSpare
WHERE SpareID IN
(SELECT s.SpareID
FROM TrainingDB.dbo.LocationsPerSpare s
WHERE s.SpareLocationID = SpareLocationID
AND s.SpareID = SpareID
GROUP BY s.SpareID
HAVING COUNT(CONVERT(VARCHAR(36), s.SpareID)) > 2)
ORDER BY SpareID
我觉得这很奇怪。有任何想法吗?