Hi, I hope this is the right place to post this. I am pretty much stuck but think I am doing pretty good so far as I am getting more and more into SQL
using SQL Server 2000 here.
I want to be able to get the last username who replied to a topic. The "DateOfLastReply" works great, it gives me the last date of a reply to a topic so pretty much along with this, I want to get who it was that replied last. Any ideas so far, I have this but it isnt correct:
SELECT Threads.[ID] 'ThreadID', ThreadName, DateOfCreation 'DateCreated', (SELECT TOP 1 DateOfReply FROM Replies WHERE Replies.ThreadID = Threads.[ID] ORDER BY DateOfReply DESC) 'LastReplyDate', (SELECT TOP 1 e.Username FROM Replies, Threads WHERE Replies.UserID = e.[ID] AND Threads.[ID] = Replies.ThreadID AND Threads.ThreadStarterUser = e.[ID] ORDER BY DateOfReply DESC) 'LastUserReplied', --HERE (SELECT COUNT(ReplyID) FROM Replies WHERE ThreadID = Threads.[ID]) 'NumberOfReplies', e.username 'UsernameCreator' FROM Threads INNER JOIN Users e ON e.[ID] = ThreadStarterUser |

Query help - get last username?
Swapnil Mahankal
r1.DateOfReply AS "LastReplyDate",
a.Username AS "LastUserReplied",
r1."NumberOfReplies",
e.username AS "UsernameCreator"
FROM Threads
INNER JOIN Users e ON
e.[ID] = ThreadStarterUser
David Lowe
This seems to work, but I am wondering if perhaps there is a better way
SELECT Threads.[ID] 'ThreadID', ThreadName, DateOfCreation 'DateCreated',
(SELECT TOP 1 DateOfReply FROM Replies WHERE Replies.ThreadID = Threads.[ID] ORDER BY DateOfReply DESC) 'LastReplyDate',
(SELECT TOP 1 a.Username FROM Replies
INNER JOIN Users a ON
a.[ID] = Replies.UserID
WHERE Replies.ThreadID = Threads.[ID] AND a.[ID] = Replies.UserID
ORDER BY DateOfReply DESC) 'LastUserReplied',
(SELECT COUNT(ReplyID) FROM Replies WHERE ThreadID = Threads.[ID]) 'NumberOfReplies',
e.username 'UsernameCreator'
FROM Threads
INNER JOIN Users e ON
e.[ID] = ThreadStarterUser
Peter N Roth
jeffps
Thank-you for your valuable help! Much appreciated. I am actually using SQL Server 2000 but hopefully the query you have given should also work...
Edit: doesnt work for SQL Server 2000 - Unfortunatly the hosting Server only has SQL Server 2000 :(
Could I ask - why should we be using the AS keyword with 'alias' What is the difference between doing that, and just giving it the alias for a field name - I am just curious for my knowledge
Mr_Gear
Thats great, thank-you for your valuable help and advice!
Have a great day