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

  • Home
  • SEARCH
  • 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 8909761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:30:52+00:00 2026-06-15T03:30:52+00:00

I have table like this: type | date | id —————————– 1 | 2012-01-01

  • 0

I have table like this:

type       | date       | id
-----------------------------
1          | 2012-01-01 | 1
2          | 2012-01-01 | 2
1          | 2012-02-02 | 3
2          | 2012-02-02 | 4

I need to build query that will pick all “up-to-date” distinct values of type ( in this example it will be records with id’s 3 and 4). Now i have this solution :

select * from test t1 where date = 
(select max(date) from test t2 where t2.type = t1.type ) order by type, date desc

I am embarrassed by the presence of nested select, maybe there is more elegant 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-15T03:30:54+00:00Added an answer on June 15, 2026 at 3:30 am

    since you didn’t mention the RDBMS you are using, try this one. will work on most RDBMS.

    SELECT  a.*
    FROM    tableName a
            INNER JOIN
            (
                SELECT  type, MAX(DATE) maxDAte
                FROM tableName
                GROUP BY type
            ) b ON a.type = b.type AND
                    a.DATE = b.maxDate
    
    • SQLFiddle Demo

    or if you RDBMS supports Window Function

    SELECT type, date, id
    FROM
    (
        SELECT type, date, id,
                ROW_NUMBER() OVER (PARTITION BY type
                                    ORDER BY date DESC) rn
        FROM tableNAme
    ) s
    WHERE rn = 1
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data in my database that looks like this: Date (DateTime) Type (varchar)
I have a transaction table like this: TransactionID Date AccountNumber Type Amount 657520 02-07-1999
I have a table like this: num | type -----+------ 123 | 3 123
i have a table like this: Table(MissioneID, Type) Type can be 1,2 or 3
I have a table like this in my database (SQL Server 2008) ID Type
I have a MySQL table structured somewhat like this: type name value ===================== 1
I have a table like this : +-------+------------+------+-----+---------+-------+ | Field | Type | Null
I have this mysql table called comments which looks like this: commentID parentID type
Let's say I have a table like this in my DB Date ItemId Count
Currently I have an mysql table like this: +---------+----------+------+-----+---------+----------------+ | Field | Type |

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.