Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8258101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:25:22+00:00 2026-06-08T02:25:22+00:00

This statement returns 2 rows: SELECT ts.UnitId,ts.TeststepId, MAX(ts.CreatedAt)as CreatedAt FROM Teststep ts INNER JOIN

  • 0

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:

enter image description here

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:25:24+00:00Added an answer on June 8, 2026 at 2:25 am
    SELECT TOP (1) ts.UnitId, ts.TeststepId, ts.CreatedAt 
      FROM dbo.Teststep AS ts
      INNER JOIN Unit AS u 
      ON ts.UnitId = u.UnitId
      WHERE u.TemplateId = 2 
      ORDER BY ts.CreatedAt DESC;   
    

    If you want a row per UnitId, and SQL Server 2005+, then:

    ;WITH x AS 
    (
      SELECT ts.UnitId, ts.TestStepId, ts.CreatedAt,
        rn = ROW_NUMBER() OVER (PARTITION BY ts.UnitId ORDER BY ts.CreatedAt DESC)
      FROM dbo.Teststep AS ts
      INNER JOIN Unit AS u 
      ON ts.UnitId = u.UnitId
      WHERE u.TemplateId = 2 
    )
    SELECT UnitId, TestStepId, CreatedAt
      FROM x
      WHERE rn = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example this SQL statement returns 1k rows SELECT * FROM tableName WHERE someCondition
Why this sql statement returns no row in SQL 2005? select 1 from tblMessage
How do I get this statement (which returns duplicates): Select Name from Table To
If I use this statement: $query = select * from Products where category_description='.$search_dept.' ORDER
We have this statement: (SELECT res_bev.bev_id, property_value.name AS priority FROM res_bev, bev_property, property_value WHERE
if select * from table where x=1 returns 0 rows, then I need select
A continuation from this question I need a SQL statement that returns the number
I am running a select statement in a report that returns 30 rows in
I have a table with 53 rows. My sql statement looks like this: SELECT
This is a follow up question from Calling constructor in return statement . This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.