简体   繁体   English

SQL 非唯一条目

[英]SQL Non unique entries

I am trying to write a query that returns me the DateCode and ContainerName from two joined tables but only give me the records where DateCode is not unique (has multiple entries) I feel like the SQL query I've written should do exactly that but I keep getting this error:我正在尝试编写一个查询,从两个连接的表中返回 DateCode 和 ContainerName 但只给我 DateCode 不唯一的记录(有多个条目)我觉得我写的 SQL 查询应该完全那样做,但我不断收到此错误:

Each GROUP BY expression must contain at least one column that is not an outer reference.每个 GROUP BY 表达式必须至少包含一个不是外部引用的列。

Perhaps I'm just too green to understand why this isn't working but here is my query:也许我太绿了,无法理解为什么这不起作用,但这是我的查询:

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
                    );

Problem with subquery , DateCode should be from subquery query : subquery问题, DateCode应该来自子查询查询:

WHERE LA.DateCode IN (SELECT AL.DateCode 
                      FROM CamstarSch.A_LotAttributes AS AL
                      GROUP BY AL.DateCode 
                      HAVING COUNT(*) > 1
                    );

However, simple aggregation would also work :但是,简单的聚合也可以:

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;

This is a pretty easy one, you were so close!这是一个非常容易的,你是如此接近!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM