sql查询最大事务

时间:2015-06-29 07:41:27

标签: mysql sql

我有这样的表

enter image description here

我想选择Acct_id,已完成最大交易次数的用户的交易次数。

select Acct_id,max(num) From (select Acct_id, count(Acct_id) as num from Transactions group by Acct_id);

我的上面的SQL查询给了我错误 - 每个派生表都必须有自己的别名

  

为什么我的查询在没有Alias的情况下失败了?

5 个答案:

答案 0 :(得分:0)

您需要为子选择提供别名:

SELECT ...
 FROM (SELECT ...) AS anyAlias
 GROUP BY ...;

答案 1 :(得分:0)

您的解决方案:

  select Acct_id,max(num) From (select Acct_id, count(Acct_id) as num 
  from Transactions group by Acct_id) as alias;

另一种解决方案:

select count(*) as total from transactions 
group by acct_id 
order by total desc limit 0,1 

答案 2 :(得分:0)

您的子选择表需要别名:

select 
    Acct_id,
    max(num) 
From (
    select 
        Acct_id, 
        count(Acct_id) as num 
    from Transactions 
    group by Acct_id
) as temp;

答案 3 :(得分:0)

您的查询必须是: -

SELECT Acct_id, MAX(num) 
FROM (SELECT Acct_id, COUNT(Acct_id) AS num 
      FROM Transactions 
      GROUP BY Acct_id) TAB;

答案 4 :(得分:0)

您的查询是正确的,但问题是没有speciry子查询表名称likethis

select Acct_id,max(num) From (select Acct_id, count(Acct_id) as num from Transactions group by Acct_id) a;