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 8733545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:40:10+00:00 2026-06-13T09:40:10+00:00

I have a table like this Col1 Col2 Mindate MaxDate A B 12-04-2012 00:12:00:087

  • 0

I have a table like this

Col1   Col2  Mindate                        MaxDate 
A       B    12-04-2012 00:12:00:087        31-04-2012 00:00:01:001
C       D    01-01-2011                     30-01-2011 
A       B    09-05-2012                     27-05-2012
A       B    11-07-2012                     21-07-2012

I have only inserted datetime in first row but omitied in other rows for brevity, all date columns are datetime.

Assume that I have inserted the last records for A and B (Col1 and Col2 are combined key).
Now I want last record for A amd B based on the mindate which in above case should be

A       B    09-05-2012                     27-05-2012

I am trying the following query:

SELECT * FROM tbl WHERE Col1 ='A' AND Col2='B' AND ....
  • 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-13T09:40:11+00:00Added an answer on June 13, 2026 at 9:40 am

    You can use ROW_NUMBER:

    WITH CTE AS
    (
       SELECT Col1, Col2, Mindate, MaxDate
        , RN = ROW_NUMBER() OVER (PARTITION BY Col1, Col2 ORDER BY Mindate DESC)
       FROM dbo.Tbl
       WHERE COL1='A' AND COL2='B'
    )
    SELECT Col1, Col2, Mindate, MaxDate
    FROM CTE 
    WHERE RN = 2
    

    This would return the second last record according to Mindate and the given key.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with 4 columns like this: Date Col1 Col2 Col3 27/03/2012
I have a table that looks like this: ID (pk,int) Col1 (nvarchar) Col2 (nvarchar)
I have a table like this Table1 Col1 Col2 Col3 Col4 Col5 Col6 Col7
I have a table that looks like this: Col1 Col2 Col3 Col4 a b
I have a table that looks like this: id, col1, col2 col3 0 Goat
I have table like this col1 col2 col3 3 5 8 4 5 5
I have a table like this: col1 col2 2001 3001 2002 3001 2003 3001
I've got a #TempTble which looks like this: Col1,Col2,5,8,19,.... Also, I have another table
I have a table like this in an excel spreadshhet: Col1 | Col2 |
Lets say i have a table like this: col1 col2 col3 row1 1 null

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.