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

The Archive Base Latest Questions

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

I have 2 tables to join in a specific way. I think my query

  • 0

I have 2 tables to join in a specific way. I think my query is right, but not sure.

select t1.userID, t3.Answer Unit, t5.Answer Demo   FROM         table1 t1       inner join (select * from table2) t3 ON t1.userID = t3.userID        inner join (select * from table2) t5 ON t1.userID = t5.userID   where       NOT EXISTS (SELECT * FROM table1 t2 WHERE t2.userID = t1.userID AND t2.date > t1.date)       and NOT EXISTS (SELECT * FROM table2 t4 WHERE t4.userID = t3.userID and t4.counter > t3.counter)       and NOT EXISTS (SELECT * FROM table2 t6 WHERE t6.userID = t5.userID and t6.counter > t5.counter)       and t1.date_submitted >'1/1/2009'       and t3.question = Unit       and t5.question = Demo   order by       t1.userID   

From table1 I want distinct userID where date > 1/1/2009

table1       userID    Date 1         1/2/2009   1         1/2/2009   2         1/2/2009   3         1/2/2009   4         1/1/2008   

So The result I want from table1 should be this:

userID   1   2   3   

I then want to join this on userID with table2, which looks like this:

table2  userID    question   answer   counter 1         Unit       A        1 1         Demo       x        1 1         Prod       100      1 2         Unit       B        1 2         Demo       Y        1 3         Prod       100      1 4         Unit       A        1 1         Unit       B        2 1         Demo       x        2 1         Prod       100      2 2         Unit       B        2 2         Demo       Z        2 3         Prod       100      2 4         Unit       A        2 

I want to join table1 with table2 with this result:

userID    Unit    Demo 1         B       X 2         B       Z 

In other words,
select distinct userID from table2 where question = Unit for the highest counter
and then
select distinct userID from table2 where question = Demo for the highest counter.

I think what I’ve done is 3 self-joins then joined those 3 together.

Do you think it’s right?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T14:06:24+00:00Added an answer on May 11, 2026 at 2:06 pm
    SELECT  du.userID, unit.answer, demo.answer FROM    (         SELECT  DISTINCT userID         FROM    table1         WHERE   date > '1/1/2009'         ) du LEFT JOIN         table2 unit ON      (userID, question, counter) IN         (         SELECT  du.userID, 'Unit', MAX(counter)         FROM    table2 td         WHERE   userID = du.userID                 AND question = 'Unit'         ) LEFT JOIN         table2 demo ON      (userID, question, counter) IN         (         SELECT  du.userID, 'Demo', MAX(counter)         FROM    table2 td         WHERE   userID = du.userID                 AND question = 'Demo'         ) 

    Having an index on table2 (userID, question, counter) will greatly improve this query.

    Since you mentioned SQL Server 2005, the following will be easier and more efficient:

    SELECT  du.userID,         (         SELECT  TOP 1 answer         FROM    table2 ti         WHERE   ti.user = du.userID                 AND ti.question = 'Unit'         ORDER BY                 counter DESC         ) AS unit_answer,         (         SELECT  TOP 1 answer         FROM    table2 ti         WHERE   ti.user = du.userID                 AND ti.question = 'Demo'         ORDER BY                 counter DESC         ) AS demo_answer FROM    (         SELECT  DISTINCT userID         WHERE   date > '1/1/2009'         FROM    table1         ) du 

    To aggregate:

    SELECT  answer, COUNT(*) FROM    (         SELECT  DISTINCT userID         FROM    table1         WHERE   date > '1/1/2009'         ) du JOIN    table2 t2 ON      t2.userID = du.userID         AND t2.question = 'Unit' GROUP BY         answer 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in my database table1, table2. I have a join query
I am not sure why this behaves this way. I need to select few
I have two tables which I want to join together using a left outer
I have 3 tables I need to join. The contracts table is the main
I have two tables (Table A, Table B) joined with a join table (TableAB)
I have two tables user and log. I would like to join the user
In a MySQL database I have two tables linked in a join. One table
I have 2 tables, faq and faq_categories, and I want to join them to
I have two tables, tbl_foo and tbl_bar , and I want to join these
I have 4 different tables that I want to join. The tables are structured

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.