如何计算SQL中的最频繁值

如何计算SQL中的最频繁值,sql,postgresql,count,aggregate-functions,greatest-n-per-group,Sql,Postgresql,Count,Aggregate Functions,Greatest N Per Group,我有一个表,它有三列:货币,交易这种货币的交易所,日期 Currency Exchange Date USD NewYork 01/12/20 USD NewYork 01/11/20 USD NewYork 01/10/20 USD Montreal 01/10/20 CAD Montreal 01/07/20 CAD Montreal 01/06/20 CAD Beijing 01/06/20 我

我有一个表,它有三列:货币,交易这种货币的交易所,日期

Currency  Exchange Date
USD       NewYork  01/12/20
USD       NewYork  01/11/20
USD       NewYork  01/10/20
USD       Montreal 01/10/20
CAD       Montreal 01/07/20
CAD       Montreal 01/06/20
CAD       Beijing  01/06/20
我需要回答一个问题,哪家交易所是这种特殊货币的领导者

这意味着对于给定的货币,计算要交换的记录数 并且只返回最大值 换句话说,查询的结果应该是

Currency Exchange Frequency 
USD      NewYork  3
CAD      Montreal 2 

如果要在聚合查询中使用最常见的行,请使用窗口函数:

select ce.*
from (select currency, exchange, count(*) as cnt,
             rank() over (partition by currency order by count(*) desc) as seqnum
      from t
      group by currency, exchange
     ) ce
where seqnum = 1;
select *
from (
    select currency, exchange, count(*) frequency,
        rank() over(partition by currency order by count(*) desc) rn
    from mytable
    group by currency, exchange
) t
where rn = 1
select distinct currency,
       first_value(exchange) over (partition by currency order by count(*) desc) exchange,
       max(count(*)) over (partition by currency) frequency
from tablename
group by currency, exchange
注意:如果发生连接,则返回所有最大值。如果您只需要一个,请使用
行号()
而不是
rank()

编辑:

在Postgres中(在我回答后添加),您可以在上使用
distinct:

select distinct on (currency) exchange, count(*) as cnt
from t
group by currency, exchange
order by currency, count(*) desc;

请注意,如果存在关联,则不会返回重复项。

如果要在聚合查询中使用最常用的行,请使用窗口函数:

select ce.*
from (select currency, exchange, count(*) as cnt,
             rank() over (partition by currency order by count(*) desc) as seqnum
      from t
      group by currency, exchange
     ) ce
where seqnum = 1;
select *
from (
    select currency, exchange, count(*) frequency,
        rank() over(partition by currency order by count(*) desc) rn
    from mytable
    group by currency, exchange
) t
where rn = 1
select distinct currency,
       first_value(exchange) over (partition by currency order by count(*) desc) exchange,
       max(count(*)) over (partition by currency) frequency
from tablename
group by currency, exchange
注意:如果发生连接,则返回所有最大值。如果您只需要一个,请使用
行号()
而不是
rank()

编辑:

在Postgres中(在我回答后添加),您可以在
上使用
distinct:

select distinct on (currency) exchange, count(*) as cnt
from t
group by currency, exchange
order by currency, count(*) desc;

请注意,如果存在关联,则不会返回重复项。

您可以使用窗口功能:

select ce.*
from (select currency, exchange, count(*) as cnt,
             rank() over (partition by currency order by count(*) desc) as seqnum
      from t
      group by currency, exchange
     ) ce
where seqnum = 1;
select *
from (
    select currency, exchange, count(*) frequency,
        rank() over(partition by currency order by count(*) desc) rn
    from mytable
    group by currency, exchange
) t
where rn = 1
select distinct currency,
       first_value(exchange) over (partition by currency order by count(*) desc) exchange,
       max(count(*)) over (partition by currency) frequency
from tablename
group by currency, exchange

您可以使用窗口功能:

select ce.*
from (select currency, exchange, count(*) as cnt,
             rank() over (partition by currency order by count(*) desc) as seqnum
      from t
      group by currency, exchange
     ) ce
where seqnum = 1;
select *
from (
    select currency, exchange, count(*) frequency,
        rank() over(partition by currency order by count(*) desc) rn
    from mytable
    group by currency, exchange
) t
where rn = 1
select distinct currency,
       first_value(exchange) over (partition by currency order by count(*) desc) exchange,
       max(count(*)) over (partition by currency) frequency
from tablename
group by currency, exchange
使用
first\u value()
max()
窗口函数:

select ce.*
from (select currency, exchange, count(*) as cnt,
             rank() over (partition by currency order by count(*) desc) as seqnum
      from t
      group by currency, exchange
     ) ce
where seqnum = 1;
select *
from (
    select currency, exchange, count(*) frequency,
        rank() over(partition by currency order by count(*) desc) rn
    from mytable
    group by currency, exchange
) t
where rn = 1
select distinct currency,
       first_value(exchange) over (partition by currency order by count(*) desc) exchange,
       max(count(*)) over (partition by currency) frequency
from tablename
group by currency, exchange
请参阅。
结果:

使用
first\u value()
max()
窗口函数:

select ce.*
from (select currency, exchange, count(*) as cnt,
             rank() over (partition by currency order by count(*) desc) as seqnum
      from t
      group by currency, exchange
     ) ce
where seqnum = 1;
select *
from (
    select currency, exchange, count(*) frequency,
        rank() over(partition by currency order by count(*) desc) rn
    from mytable
    group by currency, exchange
) t
where rn = 1
select distinct currency,
       first_value(exchange) over (partition by currency order by count(*) desc) exchange,
       max(count(*)) over (partition by currency) frequency
from tablename
group by currency, exchange
请参阅。
结果:


在场景中,您可以在
上使用
distinct。只需分组计算即可
货币
交换
并按
货币
计数
降序排序。 因此,查询如下所示:

select
distinct on (currency)
currency,
exchange,
count(*)
from table1
group by 1,2
order by 1,3 desc

在场景中,您可以在
上使用
distinct。只需分组计算即可
货币
交换
并按
货币
计数
降序排序。 因此,查询如下所示:

select
distinct on (currency)
currency,
exchange,
count(*)
from table1
group by 1,2
order by 1,3 desc

3
7
从何而来?Gordon Linoff它们来自于它们出现在table@OlliePugh . . . 它们与您的示例数据不匹配。请用您正在运行的数据库标记您的问题:mysql、oracle、postgresql…?@GordonLinoff这不是我的示例数据lolAnd
3
7
来自何处?Gordon Linoff它们来自它们在数据库中出现的次数table@OlliePugh . . . 它们与您的示例数据不匹配。请用您正在运行的数据库标记您的问题:mysql、oracle、postgresql…?@GordonLinoff这不是我的示例数据lol