使用密集的\u rank()mysql缺少右括号

kx5bkwkv  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(405)

我不知道我的语法哪里错了。我需要根据 invoice_total ```
select *
from (
select vendor_id, invoice_total,
dense_rank () over(partition by vendor_id order by invoice_total asc)
as ranking
from invoices) a1

bsxbgnwa

bsxbgnwa1#

添加 where a1.ranking = 1 对于sql的外部部分:

select *
from (
        select vendor_id, invoice_total,
        dense_rank () over(partition by vendor_id order by invoice_total asc)
          as ranking
        from invoices) a1
where a1.ranking = 1;
w7t8yxp5

w7t8yxp52#

mysql只支持 dense_rank() 在版本8+中。始终可以使用相关子查询:

select i.*
from invoices i
where i.invoice_total = (select max(i2.invoice_total)
                         from invoices i2
                         where i2.vendor_id = i.vendor_id
                        );

这假设“顶级供应商”指的是最大的总数,这与您的sql相反。
还有其他的表达方式。我也喜欢在mysql中使用元组:

select i.*
from invoices i
where (i.vendor_id, i.invoice_total) in
          (select i2.vendor_id, max(i2.invoice_total)
           from invoices i2
           group by i2.vendor_id
          );

相关问题