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?
vbnetcoderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Please use like this.

SELECT * FROM 
(
	SELECT ID ,MAX(createdate) createdate   
	From Table
	where enddate ='12/1/89'
	GROUP BY ID
)k
INNER JOIN Table tr ON tr.createdate = k.createdate AND k.Id = tr.Id

Open in new window

0
Pawan KumarDatabase ExpertCommented:
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

Open in new window

0
Pawan KumarDatabase ExpertCommented:
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

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ValentinoVBI ConsultantCommented:
Pawan's second comment is closest to how I would solve it but not entirely what was asked.  Here's my version:

;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;

Open in new window


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
0
Pawan KumarDatabase ExpertCommented:
@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.

;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

Open in new window


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 

Open in new window

0
ThierrySysAdminCommented:
Select top 1

and sort on date?
0
Pawan KumarDatabase ExpertCommented:
@Thierry - That will not work. With your code you will only 1 record.

We need latest record for each OrderId.
0
Pawan KumarDatabase ExpertCommented:
@Author - Another option is below - USING CROSS APPLY.

SELECT u.* FROM 
(
	SELECT DISTINCT OrderID 
	From Table
	where enddate ='12/1/89'	
)k
CROSS APPLY
(
  SELECT TOP 1 * FROM Table tr 
  WHERE k.OrderID = tr.OrderID 
  ORDER BY tr.createdate DESC
)u

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ValentinoVBI ConsultantCommented:
BTW: if you need the where clause then the YYYY-MM-DD format is a better one to use:

where enddate ='1989-12-01' --first of December 1989
0
Pawan KumarDatabase ExpertCommented:
Hi ValentinoV - This is a good suggestion.

@Author please use below in the where clause..

where enddate ='1989-12-01'

Open in new window


OR

where enddate ='19891201'

Open in new window

---> This is I normally prefer.
0
vbnetcoderAuthor Commented:
ty
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.