ORACLE SELECT FROM FROM TABLE GROUP BY,ERROR"不是GROUP BY表达式"

时间:2017-07-19 14:59:24

标签: sql oracle select group-by inner-join

我正在尝试选择" UID"从查询有关用户电子邮件的重复次数,但我需要使用此电子邮件获取用户的UID,我不知道如何在Oracle SQL上执行此查询。

我先做了这个问题:

SELECT EMAIL, COUNT("UID") AS NUMREPET FROM USER
GROUP BY (EMAIL) HAVING COUNT (EMAIL) > 1;

我得到类似的东西:

EMAIL                       NUMREPET
------------------------   ----------
santiago@com.co                 3    
richard@com                     2    
jorg@hotmail.com                1    
aa@p.com                        1    

如何获取查询的UID?我试过

SELECT "UID", EMAIL, COUNT("UID") AS NUMREPET FROM USER 
GROUP BY (EMAIL) HAVING COUNT (EMAIL) > 1;

但后来我收到了这个错误" 00979。 00000 - "不是GROUP BY表达式"。

要清楚我想要得到类似的东西:

"UID"       EMAIL                       NUMREPET
-----   ------------------------   ----------
1787    santiago@com.co                 3    
1788    santiago@com.co                 3    
1789    santiago@com.co                 3    
1000    richard@com                     2    
1001    richard@com                     2           
1687    jorg@hotmail.com                1    
2001    aa@p.com                        1 

3 个答案:

答案 0 :(得分:2)

你可以

select email, count(uid) as numrepet
  from user
 group by email
 where count(uid) > 1

为您提供具有多个用户ID的电子邮件地址。

然后,您可以将其与用户表连接,以获取包含这些电子邮件的用户ID。

select user.uid, user.email, repeatedemails.numrepet
  from user
  join (select email, count(uid) as numrepet
          from user
         group by email
         where count(uid) > 1) as repeatedemails
     on user.email = repeatedemails.email

我很久没有使用过oracle了,所以具体的语法可能有些偏差,但你应该从中得到这个想法。

答案 1 :(得分:0)

我假设UID是一个唯一值,可能是主键。

您也可以按其分组。

SELECT UID,CORREO, COUNT("UID") AS NUMREPET 
FROM USUARIO 
GROUP BY UID,(CORREO) 
HAVING COUNT (CORREO) > 1;

由于只有唯一值,它会将它们分开保存在行中。

抱歉我的英语不好。

答案 2 :(得分:0)

这可能不是更快的解决方案,但这似乎不像我们想要经常运行的那种查询,所以也许这并不重要:

SELECT EMAIL, UID, count(uid) over (partition by email) AS NUMREPET 
FROM USER
where email in 
    ( select email from user group by email having count(*) > 1)
order by 1,2;