[英]max(count(*)) Error: single-group group function
Assume I have two tables: 假设我有两个表:
I need to alter this query: Show top three users who bought copies of software 我需要更改此查询: 显示购买软件副本的前三名用户
My SQL is this: 我的SQL是这样的:
select u.name, u.age, u.sex, u.email, s.selluid, max(count(u.uid)) FROM users u, sells s where u.usrid = s.selluid
Any idea about how to solve this problem? 关于如何解决此问题的任何想法? Thanks
谢谢
SELECT x.*
FROM (
SELECT u.name
, u.age
, u.sex
, u.email
, s.selluid
, COUNT(*) as t
FROM users u JOIN sells s ON u.usrid = s.selluid
GROUP BY u.name
ORDER BY COUNT(*) DESC
) x
WHERE ROWNUM <= 3
Try this 尝试这个
select u.usrid, u.name, count(s.sellid)
from users u left join sells s on u.usrid=s.selluid
group by u.usrid, u.name order by count(s.sellid) desc;
You can solve this using an aggregation subquery with row_number()
: 您可以使用带有
row_number()
的聚合子查询来解决此问题:
select u.*, s.numsales
from users u join
(select s.selluid, count(*) as numsales,
row_number() over (order by count(*) desc) as seqnum
from sells s
group by s.selluid
) s
on u.userid = s.selluid
where seqnum <= 3;
One advantage to this approach is that you can readily get all the columns from users
using just u.*
. 这种方法的一个优点是,您只需使用
u.*
就可以轻松地从users
那里获得所有列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.