【发布时间】:2021-03-19 22:32:24
【问题描述】:
我目前正在使用 Northwind 数据库,希望看到 1997 年有更多订单的公司。我被要求使用 Windows 功能,所以我写了这个
select c.customerid,
c.companyname,
rank() over (order by count(orderid) desc )
from customers c
inner join orders o on c.customerid = o.customerid
where date_part('year',orderdate) = 1997;
但是,此代码要求我将 GROUP BY 与 c.customerid 一起使用。我根本不明白为什么。假设这段代码会给我所有的客户 id 和名称,然后窗口函数会根据订单数量给他们一个排名。那么为什么要对它们进行分组呢?
【问题讨论】:
标签: sql postgresql count aggregate-functions window-functions