[英]SQL Query to select the Max sales for each customer and the date
SQL Query to find the Customer ID, their Max Purch_amt and Date when the Max Purch happened. SQL 查询以查找客户 ID、他们的 Max Purch_amt 和 Max Purch 发生的日期。 Can anyone please help?有人可以帮忙吗? Struggling a bit here.在这里有点挣扎。 Data structure attached in the Link.链接中附加的数据结构。
Maybe something like this也许是这样的
select
ord_no,
purch_amt,
ord_date
from
(
select
*,
rank() over (partition by customerid order by purch_amt desc) as r
from orders
)O
where r=1
Try this.尝试这个。 I am using a subquery to SELECT the maximum purchase amount per customer and then I added their respective date.我正在使用子查询 SELECT 每个客户的最大购买金额,然后我添加了他们各自的日期。
WITH A AS (SELECT customer_id, MAX(Purch_amt) FROM orders)
SELECT a.*, o.ord_date
FROM A
JOIN orders o ON o.customer_id = a.customer_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.