SQL how to select latest dates from all entries

This SQL Table:
CREATE TABLE [dbo].[replication2](
      [id] [smallint] IDENTITY(1,1) NOT NULL,
      [VMName] [varchar](50) NULL,
      [PrimaryHost] [varchar](50) NULL,
      [ReplicaHost] [varchar](50) NULL,
      [LastReplicaDate] [varchar](50) NULL,
      [LastReplicaDateTime] [datetime2](7) NULL,
 CONSTRAINT [PK_replication2] PRIMARY KEY CLUSTERED

This table contains thousands of rows, each row representing a single instance of VM replication such as:

id      VMName      PrimaryHost      ReplicaHost      LastReplicaDate                  LastReplicaDateTime
1608      vm1      hostA            hostB            6/13/2015 8:44:37 am      2015-06-13 08:44:37.0000000

I would like to select all [VMName, PrimaryHost, ReplicaHost] such that for each unique [VMName, PrimaryHost, ReplicaHost] I only get the latest row, based on LastReplicaDateTime.

I've tried different group-by having methods but can't come up with anything that works.

Thanks.
gateguardAsked:
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give this a whirl..
SELECT VMName, PrimaryHost, ReplicaHost, Max(LastReplicaDateTime) as max_time
FROM replication2
GROUP BY VMName, PrimaryHost, ReplicaHost
ORDER BY VMName, PrimaryHost, ReplicaHost

Open in new window


Check out my article called SQL Server GROUP BY Solutions for an image and code-heavy tutorial on GROUP BY and HAVING.

If the above doesn't meet your requirements, then you'll have to spell out in greater detail what are are asking.  Preferably with before-and-after data mockups.
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
PortletPaulfreelancerCommented:
I recommend using ROW_NUMBER() when you want the whole row associated with a "latest" (or "earliest")  date

While grouping will efficiently determine the latest date, it is less efficient when it comes to getting the whole row associated with that date. Whereas when using ROW_NUMBER() this isn't a problem and is efficient.

SELECT
      VMName, PrimaryHost, ReplicaHost, LastReplicaDateTime
FROM (
      SELECT
            ROW_NUMBER() OVER (PARTITION BY VMName, PrimaryHost, ReplicaHost
                               ORDER BY LastReplicaDateTime DESC) AS rn
          , VMName
          , PrimaryHost
          , ReplicaHost
          , LastReplicaDateTime
      FROM replication2
      ) R
WHERE rn = 1

Open in new window

0
HuaMin ChenProblem resolverCommented:
Try
...
group by Name, PrimaryHost, ReplicaHost,max(lastreplicadate),max(lastreplicadatetime)

Open in new window

0
awking00Commented:
I agree with the row_number() method since it will also allow you to get the id and LastReplicaDate (i.e. the entire row) without having to include them in a group by clause.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
gateguard, do you still need help with this question?
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
Microsoft SQL Server

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.