使用DENSE_RANK()MySQL缺少右括号

时间:2018-06-09 23:33:08

标签: mysql sql mysql-workbench

我不确定我的语法在哪里错了。我需要根据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

2 个答案:

答案 0 :(得分:2)

为SQL的外部添加df1.join(df2, on=['a'], how='inner').filter(df1.b == df2.b)

where a1.ranking = 1

答案 1 :(得分:2)

MySQL仅支持版本8+中的dense_rank()。您始终可以使用相关子查询:

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
          );