Solved

Sorting INNER JOIN

Posted on 2004-09-04
7
409 Views
Last Modified: 2007-12-19
My problem lies here:

I have a message board running on MS-SQL. I want to sort the topics in order of last reply. I have a TOPICS table, USERNAMES table and a POSTS table. To display an asp page, I need to get the thread title, thread id, username of last person to reply and order these results by time of last reply. Ideally, this should be one sql statement but I can't get it to work.

I'm assuming this is going to take a subquery to get the latest post where POSTS.thread_id is the same as the thread id.

Can anyone help?
0
Comment
Question by:thefritterfatboy
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Expert Comment

by:ram2098
ID: 11980794
I am not sure of your table structures..but your query should be something like this.

I assume TOPICS and POSTS table are related bt THREAD_ID
and POSTS AND USERNAMES are related by USERID

then your query is as below..... (You can include a where clause to this if you want the details for a specific topic or user etc)

SELECT A.THREADID,
            A.THREADTITLE,
            C.USERNAME
FROM TOPICS A
INNER JOIN POSTS B
A.THREAD_ID = B.THREAD_ID
INNER JOIN USERNAMES C
B.USERID = C.USERID
ORDER BY B.POSTTIME DESC

If you can give your table structures in detail ...I can help you with the exact query.
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11980813
You need to use JOIN's something like this (I guessed at some of the column names). The WHERE clause uses a variable if you want to restrict it to one thread. The t, u, and p are aliases for the table names so you don't have to type the whole names over and over.

SELECT p.thread_id, p.thread_title, u.LastName
FROM Topics t
 INNER JOIN Posts p ON t.topic_id = p.topic_id
 INNER JOIN UserNames u ON p.user_id = u.user_id
WHERE p.thread_id = @thread_id
ORDER BY p.PostDate DESC

If you want the exact SQL for your specific case, we'll need you to post the column names for each table, and which columns are primary and foreign keys.
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11980815
Sorry, ram, I must have started right before you posted...
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 10

Author Comment

by:thefritterfatboy
ID: 11980833
That's going to give me a massive recordset of threads mixed in with posts. I only need one record per thread, that's where my problem lies.

The statement SELECT TOP 1 PostDate, postUserID FROM POSTS ORDER BY PostDate DESC; needs to grab the last post and use that if you get what I mean.
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11980852
Whoops, I think ram and I both missed that you only want the user name of the last person to post, so I think you'll need a couple of nested sub-selects, similar to this.

SELECT DISTINCT p.thread_id, p.thread_title, u.LastName
FROM Topics t
 INNER JOIN Posts p ON t.topic_id = p.topic_id
 INNER JOIN (
   --get user name of last person to post to a thread
   SELECT thread_id, username
   FROM Posts p INNER JOIN (
      --last post per thread
      SELECT topic_id, Max(thread_id) as Max_thread_id
      FROM Posts
      GROUP BY topic_id
      ) sub ON p.topic_id = sub.topic_id AND p.thread_id = sub.Max_thread_id
   ) u ON p.user_id = u.user_id
WHERE p.thread_id = @thread_id --optional line
0
 
LVL 11

Accepted Solution

by:
ram2098 earned 500 total points
ID: 11980913
you can write it this way too...

SELECT A.THREADID,
            A.THREADTITLE,
            C.USERNAME
FROM TOPICS A
INNER JOIN POSTS B
A.THREAD_ID = B.THREAD_ID
INNER JOIN USERNAMES C
B.USERID = C.USERID
WHERE B.POSTTIME = (SELECT MAX(POSTTIME) FROM POSTS WHERE THREAD_ID = A.THREAD_ID)
ORDER BY B.POSTTIME DESC
0
 
LVL 10

Author Comment

by:thefritterfatboy
ID: 11983851
Using ram2098's sugeestion the solution appears to lie in this SQL:

SELECT T.thread_id,
       T.thread_title,
       U.username,
       U.user_id,
       P.post_time
FROM TVERSION4 T
INNER JOIN PVERSION4 P ON
P.from_thread_id = T.thread_id
INNER JOIN USERS U
ON P.post_user_id = U.user_id
WHERE P.post_time = (SELECT MAX(S.post_time) FROM PVERSION4 S
WHERE S.from_thread_id = T.thread_id)
ORDER BY P.post_time DESC

jdlambert - I'm accepting ram's solution as it makes less queries, space and looks neater! I appreciate your time and effort, however.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Microsoft SQL query 7 39
SQL Backup skipping a few tables 7 25
SQL Connection (Error 18456) 14 30
SQL Server memory Issue 7 74
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now