SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 left join Distinct alternative
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

LittlePaint
Starting Member

2 Posts

Posted - 09/06/2012 :  14:29:01  Show Profile  Reply with Quote
I have the following query:

SELECT X.MigrationID, MachineName, X.MessagePostDateEST, Y.CommentText FROM TABLE1 AS X
LEFT JOIN TABLE2 AS Y ON X.MigrationID=Y.MigrationID
WHERE X.MessagePostDateEST = (SELECT MAX(CAST(MessagePostDateEST AS datetime)) FROM TABLE1 WHERE MigrationID=X.MigrationID)
ORDER BY CAST(X.MessagePostDateEST AS datetime) DESC

Which produces results like this:
MigrationID MachineName MessagePostDateEST CommentText
{103310B73F8A} LM74 09/06/2012 11:46:24.903 AM NULL
{40050427F350} LV82 09/06/2012 11:30:53.967 AM Comment 1
{40050427F350} LV82 09/06/2012 11:30:53.967 AM Comment 2
{40050427F350} LV82 09/06/2012 11:30:53.967 AM Comment 3
{72DF3E618FE0} LM60 09/06/2012 10:29:07.020 AM NULL

What I need is a way to modify the query so that it will only return one record that has multiple comments associated with it. For example, I want results that look like this:
MigrationID MachineName MessagePostDateEST CommentText
{103310B73F8A} LM74 09/06/2012 11:46:24.903 AM NULL
{40050427F350} LV82 09/06/2012 11:30:53.967 AM Comment 1
{72DF3E618FE0} LM60 09/06/2012 10:29:07.020 AM NULL

chadmat
The Chadinator

USA
1947 Posts

Posted - 09/06/2012 :  15:00:04  Show Profile  Visit chadmat's Homepage  Reply with Quote
Try this...I am randomly selecting the MAX() comment since you gave no requirement for how to decide which one to pick.

SELECT X.MigrationID, MachineName, X.MessagePostDateEST, Y.CommentText FROM TABLE1 AS X
LEFT JOIN (SELCT MigrationID, MAX(CommentText) FROM TABLE2 GROUP BY MigrationID) Y ON X.MigrationID=Y.MigrationID
WHERE X.MessagePostDateEST = (SELECT MAX(CAST(MessagePostDateEST AS datetime)) FROM TABLE1 WHERE MigrationID=X.MigrationID)
ORDER BY CAST(X.MessagePostDateEST AS datetime) DESC


-Chad
Go to Top of Page

LittlePaint
Starting Member

2 Posts

Posted - 09/13/2012 :  11:08:17  Show Profile  Reply with Quote
Chad, many thanks, that is exactly what I was looking for. I only had to add a column alias from what you posted in order to have it work:

SELECT X.MigrationID, MachineName, X.MessagePostDateEST, Y.MCT FROM TABLE1 AS X
LEFT JOIN (SELECT MigrationID, MAX(CommentText) AS MCT FROM TABLE2 GROUP BY MigrationID) AS Y ON X.MigrationID=Y.MigrationID
WHERE X.MessagePostDateEST = (SELECT MAX(CAST(MessagePostDateEST AS datetime)) FROM TABLE1 WHERE MigrationID=X.MigrationID)
ORDER BY CAST(X.MessagePostDateEST AS datetime) DESC
Go to Top of Page

chadmat
The Chadinator

USA
1947 Posts

Posted - 09/13/2012 :  12:35:12  Show Profile  Visit chadmat's Homepage  Reply with Quote
You are welcome

-Chad
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000