我有一个包含信息的表格:
customer_id | Date | Time| Sales
我们在给定的一天内有多个客户的交易。我如何在任何一天检索每个客户的倒数第五笔交易?
试试这个:
SELECT A.customer_id, A.Date, A.Time, A.Sales
FROM (SELECT A.customer_id, A.Date, A.Time, A.Sales,
IF(@customerId = @customerId:=A.customer_id AND @Date = @Date:=A.Date, @Id:=@ID + 1, @ID:=1) AS RowNum
FROM tableA A, (SELECT @customerId:=0, @Date:='', @Id:=0) AS B
ORDER BY A.customer_id, A.Date, A.Time DESC
) AS A
WHERE A.RowNum = 5