PostgreSQL在每个日期范围内选择每个客户的最后订单 [英] PostgreSQL SELECT the last order per customer per date range

查看:79
本文介绍了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

仅此而已。

这篇关于PostgreSQL在每个日期范围内选择每个客户的最后订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆