V
V
vika19972021-10-08 19:01:47
SQL Server
vika1997, 2021-10-08 19:01:47

How to find the date of the previous order and before the previous order?

I can not find the location of the date of the previous order and before the previous order. There is 1 client and he has 3 orders. It is necessary to display the date of his order, the date of his previous order and the date of his previous order.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2021-10-08
@vika1997

SELECT TOP(3) [DateOrder] FROM [Order] WHERE ([Client] = @ID) AND ([DateOrder] <= @DT)
  ORDER BY [DateOrder] DESC

S
Stalker_RED, 2021-10-08
@Stalker_RED

select count(*) from orders where userId=42;  -- кол-во заказов

select count(*) from orders where userId=42 and date < "2021-10-05" ;  -- кол-во заказов до даты

select * from orders where usetId=42
ORDER BY date DESC -- сортировать по дате в обратном порядке
LIMIT 3; -- три последние записи

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question