SQL 非唯一条目
SQL Non unique entries
我正在尝试编写一个查询,returns 我从两个连接的表中获取 DateCode 和 ContainerName,但只给我 DateCode 不唯一的记录(有多个条目)
我觉得我写的 SQL 查询应该做到这一点,但我一直收到这个错误:
Each GROUP BY expression must contain at least one column that is not an outer reference.
也许我太菜鸟了,无法理解为什么这不起作用,但这是我的查询:
SELECT LA.DateCode, C.ContainerName
FROM CamstarSch.A_LotAttributes LA INNER JOIN
CamstarSch.Container C
ON C.ContainerId = LA.ContainerId
WHERE LA.DateCode IN (SELECT LA.DateCode
FROM CamstarSch.A_LotAttributes
GROUP BY LA.DateCode, C.ContainerName
HAVING COUNT(*) > 1
);
这很简单,你太接近了!
SELECT LA.DateCode, C.ContainerName, COUNT(*)
FROM CamstarSch.A_LotAttributes LA
INNER JOIN CamstarSch.Container C ON C.ContainerId = LA.ContainerId
GROUP BY LA.DateCode, C.ContainerName
HAVING COUNT(*)>1
subquery
的问题,DateCode
应该来自子查询查询:
WHERE LA.DateCode IN (SELECT AL.DateCode
FROM CamstarSch.A_LotAttributes AS AL
GROUP BY AL.DateCode
HAVING COUNT(*) > 1
);
但是,简单聚合也可以:
SELECT LA.DateCode, C.ContainerName
FROM CamstarSch.A_LotAttributes LA INNER JOIN
CamstarSch.Container C
ON C.ContainerId = LA.ContainerId
GROUP BY LA.DateCode, C.ContainerName
HAVING COUNT(*) > 1;
我正在尝试编写一个查询,returns 我从两个连接的表中获取 DateCode 和 ContainerName,但只给我 DateCode 不唯一的记录(有多个条目) 我觉得我写的 SQL 查询应该做到这一点,但我一直收到这个错误:
Each GROUP BY expression must contain at least one column that is not an outer reference.
也许我太菜鸟了,无法理解为什么这不起作用,但这是我的查询:
SELECT LA.DateCode, C.ContainerName
FROM CamstarSch.A_LotAttributes LA INNER JOIN
CamstarSch.Container C
ON C.ContainerId = LA.ContainerId
WHERE LA.DateCode IN (SELECT LA.DateCode
FROM CamstarSch.A_LotAttributes
GROUP BY LA.DateCode, C.ContainerName
HAVING COUNT(*) > 1
);
这很简单,你太接近了!
SELECT LA.DateCode, C.ContainerName, COUNT(*)
FROM CamstarSch.A_LotAttributes LA
INNER JOIN CamstarSch.Container C ON C.ContainerId = LA.ContainerId
GROUP BY LA.DateCode, C.ContainerName
HAVING COUNT(*)>1
subquery
的问题,DateCode
应该来自子查询查询:
WHERE LA.DateCode IN (SELECT AL.DateCode
FROM CamstarSch.A_LotAttributes AS AL
GROUP BY AL.DateCode
HAVING COUNT(*) > 1
);
但是,简单聚合也可以:
SELECT LA.DateCode, C.ContainerName
FROM CamstarSch.A_LotAttributes LA INNER JOIN
CamstarSch.Container C
ON C.ContainerId = LA.ContainerId
GROUP BY LA.DateCode, C.ContainerName
HAVING COUNT(*) > 1;