PostgreSQL在每个日期范围内选择每个客户的最后订单
问题描述:
在PostgreSQL中:
我有一个包含3列的表:
In PostgreSQL: I have a Table that has 3 columns:
CustomerNum,OrderNum,OrderDate
。
每个日期范围内每个客户的订单可能(也可能没有)。我需要的是每个客户在提供的日期范围内的最后一个OrderNum。
我一直在做的是获取客户的ResultSet并分别查询每个客户,但这花了太多时间。
There may(or may not) be many orders for each customer per date range. What I am needing is the last OrderNum for each Customer that lies in the date range that is supplied. What I have been doing is getting a ResultSet of the customers and querying each one separately, but this is taking too much time.
有什么办法使用子选择来选择客户,然后为每个客户获取最后的OrderNum?
Is there any way of using a sub-select to select out the customers, then get the last OrderNum for each Customer?
答
select customernum, max(ordernum)
from table
where orderdate between '...' and '...'
group by customernum
仅此而已。