This statement returns 2 rows:
SELECT ts.UnitId,ts.TeststepId, MAX(ts.CreatedAt)as CreatedAt
FROM Teststep ts
INNER JOIN Unit u ON ts.UnitId = u.UnitId
Where u.TemplateId = 2
Group by TeststepId, ts.UnitId
How can I get the one row with the highest CreatedAt date ? Should I distinct the unitId?
UPDATE
Aaron and flem aksed for more info:
1 Template has N Units
1 Unit has N Teststeps
The clustered primary key of the Teststep table is TeststepId and CreatedAt.
[TeststepId] [int] NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[PreCondition] [nvarchar](500) NOT NULL,
[TestInstruction] [nvarchar](500) NOT NULL,
[ExpectedResult] [nvarchar](500) NOT NULL,
[CreatedAt] [datetime] NOT NULL,
[CreatedBy] [nvarchar](50) NOT NULL,
[UnitId] [int] NOT NULL,
The Teststep table
sample data with the most important fields in the Teststep table could be:

The teststep shows historized teststeps. From the above image you see that the TeststepId = 4 exist 2 times. A historized version (58 sec.) and a current version (59 sec.).
I want to get only the teststepid 4 with 59 seconds to show in a “current” View.
UPDATE 2
I want to get only the teststepid 4 with 59 seconds AND
all other teststepIds with their current date. If there are only rows with different teststepids take those because they are the current rows.
I am just about to change my integration test and find about the solution.
If you want a row per UnitId, and SQL Server 2005+, then: