Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL densite_Rank()和Max()函数不在同一语句中工作_Mysql_Sql - Fatal编程技术网

MySQL densite_Rank()和Max()函数不在同一语句中工作

MySQL densite_Rank()和Max()函数不在同一语句中工作,mysql,sql,Mysql,Sql,我从这个运行良好的MySQL查询开始: select h.id, h.name, count(distinct c.sales_id) from customers h join sales c on h.id=c.id group by 1,2 order by 3 desc, 2 desc 但是,现在我想在同一条语句中添加两列: 第一列应该是“count(distinct c.sales_id)”列中的最大值-->max(count(distinct c.sales_id)) 第二列应按

我从这个运行良好的MySQL查询开始:

select 
h.id,
h.name,
count(distinct c.sales_id)
from customers h join sales c on h.id=c.id
group by 1,2
order by 3 desc, 2 desc
但是,现在我想在同一条语句中添加两列:

  • 第一列应该是“count(distinct c.sales_id)”列中的最大值-->
    max(count(distinct c.sales_id))
  • 第二列应按“计数(不同的c.sales_id)”列-->
    densite_rank()在(按计数排序(不同的c.sales_id))
  • 我很难添加这两个列,因为我在这个查询中不断遇到聚合错误:

    select 
    h.id,
    h.name,
    count(distinct c.sales_id),
    max(count(distinct c.sales_id)),
    dense_rank() over (order by count(distinct c.sales_id))
    from customers h join sales c on h.id=c.id
    group by 1,2
    order by 3 desc, 2 desc
    

    有人能帮忙吗?

    您可能很快就会找到解决方案

    在SQL中,不能最大化计数。
    但是一个人可以超过一个计数

    所以这应该在MySql 8.0中工作

    (未对样本数据进行测试)


    这背后的原因是窗口函数是在聚合函数之后处理的

    非常感谢您,但是当我使用您的方法运行查询时,我仍然得到一个错误:第1行的错误1064(42000):您的SQL语法有一个错误;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在第5行“您使用的MySQL版本是什么”中使用“(),densite_rank()over(order by count(distinct c.sales_id))。嗯,我还没有在数据上测试它。所以我可能错了。我正在使用Hackerrank上的代码编辑器进行SQL挑战。我不知道是哪个版本,我不知道。它可以运行类似于
    select@@version?有趣的是,它声明:5.7.27-0ubuntu0.18.04.1
    
    select 
    h.id,
    h.name,
    count(distinct c.sales_id) as total_uniq_sales,
    MAX(count(distinct c.sales_id)) OVER (),
    dense_rank() over (order by count(distinct c.sales_id))
    from customers h 
    join sales c on h.id=c.id
    group by h.id, h.name
    order by total_uniq_sales desc, h.name desc