vbnetcoder
asked on
return only the record with the latest date
I have a query like this
SELECT
ID
OrderID
createdate
enddate
Field1
Field3
field4
From Table
where enddate ='12/1/89'
I want to only return only 1 records per order ID and the records I want is the return the newest create date. How do I do this?
SELECT
ID
OrderID
createdate
enddate
Field1
Field3
field4
From Table
where enddate ='12/1/89'
I want to only return only 1 records per order ID and the records I want is the return the newest create date. How do I do this?
We can also use CTE..
;WITH CTE AS
(
SELECT ID ,MAX(createdate) createdate
From Table
where enddate ='12/1/89'
GROUP BY ID
)
SELECT tr.* FROM CTE k INNER JOIN Table tr ON tr.createdate = k.createdate AND k.Id = tr.Id
We can also use ranking functions for this -
;WITH CTE AS
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY createdate DESC) rnk
From Table
where enddate ='12/1/89'
)
SELECT ID,OrderID,createdate,enddate,Field1,Field3,field4
FROM CTE
WHERE rnk = 1
Pawan's second comment is closest to how I would solve it but not entirely what was asked. Here's my version:
The query assumes that the OrderID and createdate combination is unique over the whole table.
Edit: replaced "enddate" with "createdate" - also, add where clause if it is needed
;with MaxDatesPerOrderID as (
SELECT OrderID, MAX(createdate) createdate
From Table
group by OrderID
)
select * from table t
inner join MaxDatesPerOrderID md on md.OrderID = t.OrderID
and md.createdate= t.createdate;
The query assumes that the OrderID and createdate combination is unique over the whole table.
Edit: replaced "enddate" with "createdate" - also, add where clause if it is needed
@ValentinoV
- Your solution is incorrect. this is what is asked. " I want is the return the newest create date" . You are using enddate and Where clause is also missing.
@Author --updated for OrderId. Everything is same just change column Id to Orderid.
OR this
- Your solution is incorrect. this is what is asked. " I want is the return the newest create date" . You are using enddate and Where clause is also missing.
@Author --updated for OrderId. Everything is same just change column Id to Orderid.
;WITH CTE AS
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY OrderID ORDER BY createdate DESC) rnk
From Table
where enddate ='12/1/89'
)
SELECT ID,OrderID,createdate,enddate,Field1,Field3,field4
FROM CTE
WHERE rnk = 1
OR this
;WITH CTE AS
(
SELECT OrderID ,MAX(createdate) createdate
From Table
where enddate ='12/1/89'
GROUP BY OrderID
)
SELECT tr.* FROM CTE k INNER JOIN Table tr ON tr.createdate = k.createdate AND k.OrderID = tr.OrderID
Select top 1
and sort on date?
and sort on date?
@Thierry - That will not work. With your code you will only 1 record.
We need latest record for each OrderId.
We need latest record for each OrderId.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi ValentinoV - This is a good suggestion.
@Author please use below in the where clause..
OR
@Author please use below in the where clause..
where enddate ='1989-12-01'
OR
where enddate ='19891201'
---> This is I normally prefer.
ASKER
ty
Open in new window