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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:43:53+00:00 2026-06-15T11:43:53+00:00

I have a table that uses two identifying columns, let’s call them id and

  • 0

I have a table that uses two identifying columns, let’s call them id and userid. ID is unique in every record, and userid is unique to the user but is in many records.

What I need to do is get a record for the User by userid and then join that record to the first record we have for the user. The logic of the query is as follows:

SELECT v1.id, MIN(v2.id) AS entryid, v1.userid
FROM views v1
INNER JOIN views v2
  ON v1.userid = v2.userid

I’m hoping that I don’t have to join the table to a subquery that handles the min() piece of the code as that seems to be quite slow.

  • 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-15T11:43:54+00:00Added an answer on June 15, 2026 at 11:43 am

    I guess (it’s not entirely clear) you want to find for every user, the rows of the table that have minimum id, so one row per user.

    In that case, you an use a subquery (a derived table) and join it to the table:

    SELECT v.*
    FROM views AS v
      JOIN
        ( SELECT userid, MIN(id) AS entryid
          FROM views
          GROUP BY userid
        ) AS vm
        ON  vm.userid = v.userid 
        AND vm.entryid = v.id ;
    

    The above can also be written using a Common Table Expression (CTE), if you like them:

    ; WITH vm AS
        ( SELECT userid, MIN(id) AS entryid
          FROM views
          GROUP BY userid
        )
      SELECT v.*
      FROM views AS v
        JOIN vm
          ON  vm.userid = v.userid 
          AND vm.entryid = v.id ;
    

    Both would be quite efficient with an index on (userid, id).

    With SQL-Server, you could write this using the ROW_NUMBER() window function:

    ; WITH viewsRN AS
        ( SELECT *
               , ROW_NUMBER() OVER (PARTITION BY userid ORDER BY id) AS rn
          FROM views
        ) 
      SELECT *                      --- skipping the "rn" column
      FROM viewsRN
      WHERE rn = 1 ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

following on from my other post... I have a table that uses two fields
I have a web page that uses jquery ajax to grab the table markup
I have content management system application that uses a polymorphic tree table as the
I am working on a program that uses two tables. The first table has
I have a legacy database that uses a table per class hierarchy inheritance strategy
I have an OS X app that uses a splitview with two embeded NSTableViews.
I have rails application that has two view ports that uses the same data.
I have two views that returns the same columns, but different rows. I am
I have a table that uses a SET datatype for one of the fields,
I have a web-app that uses EF5. I have two tables that are my

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.