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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:25:35+00:00 2026-06-11T01:25:35+00:00

I am writing a query to grab the items that a specific user_id was

  • 0

I am writing a query to grab the items that a specific user_id was the first to use. Here is some sample data –

item_id used_user_id    date_used
1       1               2012-08-25
1       2               2012-08-26               
1       3               2012-08-27
2       2               2012-08-27
3       1               2012-08-27
4       1               2012-08-21
4       3               2012-08-24
5       3               2012-08-23

query

select item_id as inner_item_id, ( select used_user_id 
                                   from test 
                                   where test.item_id = inner_item_id 
                                   order by date_used asc 
                                   limit 1 ) as first_to_use_it 
from test 
where used_user_id = 1 
group by item_id

It returns the correct values

inner_item_id   first_to_use_it
1               1
3               1
4               1

but the query is VERY slow on a giant table. Is there a certain index that I can use or a better query that I can write?

  • 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-11T01:25:37+00:00Added an answer on June 11, 2026 at 1:25 am

    i can’t get exactly what you mean because in your inner query you have sorted it by their used_user_id and and on your outer query you have filtered it also by their userid. Why not do this directly?

    SELECT DISTINCT item_id AS inner_item_id,
           used_user_id AS first_to_use_it 
    FROM   test
    WHERE  used_user_id = 1 
    

    UPDATE 1

    SELECT  b.item_id, 
            b.used_user_id AS first_to_use_it
    FROM
        (
            SELECT item_ID, MIN(date_used) minDate
            FROM tableName
            GROUP BY item_ID
        ) a
            INNER JOIN tableName b
                ON a.item_ID = b.item_ID AND
                    a.minDate = b.date_used
    WHERE   b.used_user_id = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Log and LogItem tables; I'm writing a query to grab some data
I'm writing a query that inserts customer data into a MSSQL database. Very basic.
I need help writing a query that will validate a first expression and than,
I'm writing a query to summarize some data. I have a flag in the
I am having trouble writing query so that I can query the content of
I was successful in writing the query that lists salesmen that did sell to
I am writing a query for a sql input statement, i noticed that when
I'm writing a query that has to count the number of students enrolled in
I writing a query within ms sql server 2005 and having some trouble getting
I'm writing a query that looks like this: var TheOutput = (from x in

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.