Reputation: 117
I have a table in SQL Server, the format is as follows:
I would want to get rows according to the following conditions:
Customer_ID
and Order_Number
then return only those rows with maximum dateSo the result in this case will be row 3, 4 and 5.
Any idea on how to achieve this using SQL query? The table has no primary or unique key.
Upvotes: 0
Views: 224
Reputation: 32003
use window function row_number()
select *
from
(
select *,
row_number() over(partition by Customer_ID,Order_Number order by date desc) as rn
from your_table
) t where rn=1
or use co-relates subquery
select *
from t
where date in (
select max(date)
from t t1
where t1.Customer_ID=t.Customer_ID and t1.Order_Number=t.Order_Number
)
Upvotes: 2