SQL Left Join eliminating duplicates (Full Version)

All Forums >> [Web Development] >> ASP and Database



Message


willard31 -> SQL Left Join eliminating duplicates (1/7/2005 23:43:21)

Maybe someone can help with the correct syntax for what I'm trying to accomplish. I believe the LEFT JOIN is the way to go, but I'm missing something.

I have a table from SupportLog that displays a list of calls to our helpdesk, sortable by LogID, LogLocation, LogDate, etc. A separate table, SupportDetail, records notes that are added to each call. LogID and SDLink link the two tables.

Some records have many notes, some have none. I would like to display a "*" next to the LogID of those that have notes, so the supporter knows to click the link and read the notes.

The following statement returns all calls and I'm able to put the "*" next to those that have notes, but the record is repeated for however many notes exist. So if LogID 29384 has 3 notes in the SupportDetail table, it is repeated 3 times in the recordset. I've tried playing with DISTINCT, RIGHT JOIN, etc to no avail. Database resides on SQL Server.

strSQL = "SELECT * FROM SupportLog LEFT JOIN SupportDetail ON (SupportLog.LogID=SupportDetail.SDLink) WHERE (LogAssigned LIKE '::LogAssigned::' AND LogComplete = ::LogComplete:: AND LogContact LIKE '::LogContact::' AND (LogShort LIKE '%::Search::%' OR LogLong LIKE '%::Search::%')) ORDER BY ::Sort:: ::Order:: "





BeTheBall -> RE: SQL Left Join eliminating duplicates (1/8/2005 13:23:59)

Without a lot of thought on this, have you tired adding a GROUP BY clause?

strSQL = "SELECT * FROM SupportLog LEFT JOIN SupportDetail ON (SupportLog.LogID=SupportDetail.SDLink) WHERE (LogAssigned LIKE '::LogAssigned::' AND LogComplete = ::LogComplete:: AND LogContact LIKE '::LogContact::' AND (LogShort LIKE '%::Search::%' OR LogLong LIKE '%::Search::%')) GROUP BY LogID ORDER BY ::Sort:: ::Order:: "




willard31 -> RE: SQL Left Join eliminating duplicates (1/8/2005 16:10:42)

If the statement was SELECT LogID rather than SELECT *, I think this would work. Adding GROUP BY LogID returns the following error (LogDate is the next field in the table after LogID)...

Database Results Error
Description: Column 'SupportLog.LogDate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Number: -2147217900 (0x80040E14)
Source: Microsoft OLE DB Provider for SQL Server





BeTheBall -> RE: SQL Left Join eliminating duplicates (1/8/2005 22:33:00)

Perhaps this will work for you?

http://www.outfront.net/spooky/adv_grouping.htm




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.078125