Alark
Alark

Reputation: 39

Get only the older order from transaction table for same customer

I need to get only the older order from transaction table for same customer

SELECT * 
FROM Order_Cust
WHERE Status IN (15)
  AND to_char(Order_date, 'yyyy') = 2020

enter image description here

I need order with order_date to appear only.

Upvotes: 0

Views: 37

Answers (2)

tohid
tohid

Reputation: 1

and u can try this!!

select * from Order_Cust
where order_date in (select max(order_date) from Order_Cust)

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521239

ROW_NUMBER is one option here:

WITH cte AS (
    SELECT t.*, ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY Order_date) rn
    FROM yourTable t
)

SELECT Order_Date, CustomerID, Order_Type
FROM cte
WHERE rn = 1;

Upvotes: 1

Related Questions