DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2006
    Posts
    13

    Trouble understanding query behavior - help appreciated.

    Good Morning,

    I'm really confused and could use some insight. This query involves two tables in a SQL2005 database. The tables are tblCalendarMonths and tblProjects. tblCalendarMonths consists of columns for a rowID, a startdate, an enddate, a month number (1-12), the year, and the name of the month spelled out - called monthtext. The relevant columns in tblProjects DateRequested, which is a date format, and ProjectTeam, which is an integer identifying which team is handling it. I need to be able to count the projects in a given month and filter by year and by team so that each month is displayed, even if that month's count is zero, and the counts for all of the months are only for the specified team. This query very neatly displays all of the months for the year and the counts for each month, even those that are zero:

    SELECT a.monthtext AS [Request Month], COUNT(b.DateRequested) AS [Total Requests], a.monthno AS MonthNumber
    FROM tblCalendarMonths AS a LEFT OUTER JOIN
    tblProjects AS b ON b.DateRequested >= a.startdate AND b.DateRequested < a.enddate
    WHERE (a.yearno = 2009)
    GROUP BY a.monthtext, a.monthno
    ORDER BY MonthNumber, [Request Month], [Total Requests]

    It yields a datatable that looks like this (The third column is the month number, which the results are sorted by to keep it all in proper order):

    January 0 1
    February 14 2
    March 3 3
    April 0 4
    May 1 5
    June 0 6
    July 0 7
    August 0 8
    September 10 9
    October 0 10
    November 0 11
    December 0 12

    When I try to modify the WHERE clause to filter by Project Team, it only returns the months that actually have a count, not those that are zero. Here's the modified query:

    SELECT a.monthtext AS [Request Month], COUNT(b.DateRequested) AS [Total Requests], a.monthno AS MonthNumber
    FROM tblCalendarMonths AS a LEFT OUTER JOIN
    tblProjects AS b ON b.DateRequested >= a.startdate AND b.DateRequested < a.enddate
    WHERE (a.yearno = 2009) AND (b.ProjectTeam = 1)
    GROUP BY a.monthtext, a.monthno
    ORDER BY MonthNumber, [Request Month], [Total Requests]

    It yields this datatable:

    February 11 2
    March 3 3
    May 1 5
    September 10 9

    Any ideas on why adding that one filter in the WHERE clause is causing this result? How should the query be modified to make it run correctly? Any help will be appreciated.

    Thanks.

    John

  2. #2
    Join Date
    Sep 2006
    Posts
    13
    Found the answer. I moved the "ProjectTeam = 1" filter to the ON statement and now it works just dandy. Thanks anyway.

Similar Threads

  1. query tuning
    By dhaya in forum Database
    Replies: 11
    Last Post: 08-25-2003, 05:24 PM
  2. Replies: 4
    Last Post: 02-21-2002, 03:17 PM
  3. reg...select count distinct....query
    By Mohan Ekambaram in forum Database
    Replies: 0
    Last Post: 12-09-2001, 11:56 AM
  4. reg....count distinct query
    By Mohan Ekambaram in forum Database
    Replies: 0
    Last Post: 12-08-2001, 11:21 PM
  5. ADO query timeout
    By chaumette in forum VB Classic
    Replies: 2
    Last Post: 03-31-2000, 11:09 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links