Stumped...JOIN with a DatePart on a field name...

OK...yes I am new to SQL Server 2005...I am moving T-SQL from a 7.0 server to a 2005 server and I am struggling with joins. The following is supposed to retrieve all records from the WebProxyHours table...which simply contains the numbers 1-24 and report the ClientUserName and PageHits for those in hours in which data exists in the table WebProxyLog. I have failed to make any progress so far...my code simply displays data only for those hours in which there is data. I use this data to display a 24 hour graph...showing all of the hours of the day even though they may not have had any activity.

If anyone could steer me in the right direction I would appreciate it. Thanks in advance.

T-SQL Code:

SELECT WebProxyHours.msp_hour, WebProxyLog.ClientUserName, Count(WebProxyLog.DestHost) PageHits
FROM WebProxyHours LEFT OUTER JOIN WebProxyLog on DatePart(hour, WebProxyLog.LogTime) = WebProxyHours.msp_hour
WHERE ( DatePart(month, WebProxyLog.LogDate) = DatePart(month, GetDate()) ) AND ( DatePart(day, WebProxyLog.LogDate) = DatePart(day, GetDate()) ) AND ( DatePart(year, WebProxyLog.LogDate) = DatePart(year, GetDate()) ) AND ( Left(WebProxyLog.ClientUserName,3) = 'FLM' ) AND ( WebProxyLog.mimetype = 'text/html' )
GROUP BY WebProxyHours.msp_hour, WebProxyLog.ClientUserName
ORDER BY WebProxyHours.msp_hour ASC



Answer this question

Stumped...JOIN with a DatePart on a field name...

  • MichaelZhou

    Thanks...you are right on. I guess I don't get the new JOIN syntax. Looks like I need some training. I appreciate your response.
  • Kanchi Harish

    Move the constraints in you where clause into the Join clause. These are really restrictions on the WebProxyLog table. By putting them in the Where clause, you are restricting the results after the Join has been performed.
  • Stumped...JOIN with a DatePart on a field name...