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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:28:11+00:00 2026-05-23T14:28:11+00:00

If I have a table of data like this tableid author book pubdate 1

  • 0

If I have a table of data like this

tableid    author     book                 pubdate
1          1          The Hobbit           1923
2          1          Fellowship           1925
3          2          Foundation Trilogy   1947
4          2          I Robot              1942
5          3          Frankenstein         1889
6          3          Frankenstein 2       1894

Is there a query that would get me the following without having to use a temp table, table variable or cte?

tableid    author     book                 pubdate
1          1          The Hobbit           1923
4          2          I Robot              1942
5          3          Frankenstein         1889

So I want min(ranking) grouping by person and ending up with book for that min(ranking) value.

OK, the data I gave initially was flawed. Instead of a ranking column I’ll have a date column. I need the book published earliest by author.

  • 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-05-23T14:28:12+00:00Added an answer on May 23, 2026 at 2:28 pm

    Missed that a CTE was not valid (but not sure why). How about as a subquery?

    SELECT tableid, author, book, pubdate
    FROM
    (
        SELECT 
            tableid, author, book, pubdate, 
            rn = ROW_NUMBER() OVER 
            (
                PARTITION BY author
                ORDER BY pubdate
            )
        FROM dbo.src -- replace this with the real table name
    ) AS x
    WHERE rn = 1
    ORDER BY tableid;
    

    Original:

    ;WITH x AS
    (
        SELECT 
            tableid, author, book, pubdate, 
            rn = ROW_NUMBER() OVER 
            (
                PARTITION BY author
                ORDER BY pubdate
            )
        FROM dbo.src -- replace this with the real table name
    )
    SELECT tableid, author, book, pubdate
        FROM x
        WHERE rn = 1
        ORDER BY tableid;
    

    If you want to return multiple rows when there is a tie for earliest book, use RANK() in place of ROW_NUMBER(). In the case of a tie and you only want to return one row, you need to add additional tie breaker columns to the ORDER BY within OVER().

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

Sidebar

Related Questions

I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I'm a bit confused on this. I have a data table structured like this:
I have a table like this... CustomerID DBColumnName Data 1 FirstName Joe 1 MiddleName
I like to have a query like this: select data from table where (x
I have three tables: page, attachment, page-attachment I have data like this: page ID
I have several sources of tables with personal data, like this: SOURCE 1 ID,
I have a table that has data like below(sorted by date) COL_A | COL_B
I have a table called Addim and the data looks like: TName Idate Number
I have a db table containing a column display_order . The data looks like
I have MySQL tables looking like this: regions table id | region ------------------- 1

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.