0

I have this Sql Query to show me how many task under Project but the problem it doesn't display Projects with 0 task can someone help me?

SELECT Projects.projectName AS [Project Name],
    COUNT(Projects_tasks.taskID) AS #tasks,
    Projects.projectID AS [Project ID]

FROM Projects

INNER JOIN Projects_tasks
ON Projects.projectID = Projects_tasks.projectID 

GROUP BY Projects.projectName,
    Projects_tasks.taskID,
    Projects.projectID

7 Answers 7

1

Use Left Join

SELECT     Projects.projectName AS [Project Name],
                  ISNULL(COUNT(Projects_tasks.taskID),0) AS #tasks, Projects.projectID AS [Project ID]

FROM       Projects LEFT JOIN
                  Projects_tasks ON Projects.projectID = Projects_tasks.projectID 

GROUP BY Projects.projectName,Projects.projectID

also remove the column that you are counting in the group by

Sign up to request clarification or add additional context in comments.

Comments

1

Use LEFT join, ISNULL(), and do not group by the column you are counting.

SELECT     Projects.projectName AS [Project Name],
                  ISNULL(COUNT(Projects_tasks.taskID), 0) AS #tasks, Projects.projectID AS [Project ID]

FROM       Projects LEFT JOIN
                  Projects_tasks ON Projects.projectID = Projects_tasks.projectID 

GROUP BY Projects.projectName,Projects.projectID

Comments

0

You should use a LEFT JOIN to make sure the projects with 0 tasks are also included

SELECT     Projects.projectName AS [Project Name],
           ISNULL(COUNT(Projects_tasks.taskID),0) AS #tasks, Projects.projectID AS [Project ID]
FROM       Projects LEFT JOIN
                  Projects_tasks ON Projects.projectID = Projects_tasks.projectID 

GROUP BY Projects.projectName,Projects_tasks.taskID,Projects.projectID

Comments

0

Try changing INNER JOIN to LEFT JOIN

You're not seeing 0, because there is no task to join to.

Comments

0

Use LEFT JOIN. And also COALESCE to convert null values. COALESCE must be inside the COUNT function since the column Projects_tasks.taskID can be NULL.

SELECT Projects.projectName AS [Project Name],
       COUNT(COALESCE(Projects_tasks.taskID, 0)) AS #tasks,
       Projects.projectID AS [Project ID]
FROM   Projects
         LEFT JOIN Projects_tasks
            ON Projects.projectID = Projects_tasks.projectID 
GROUP BY Projects.projectName,
    Projects_tasks.taskID,
    Projects.projectID

Comments

0

I doubt that your query will ever return more then 1 task as number of tasks. The Projects_tasks.taskID should not be in the group clause. And for your problem, you have to put LEFT JOIN instead of inner join and it will work.

SELECT Projects.projectName AS [Project Name],
    sum(case when Projects_tasks.taskID is null then 0 else 1 end) AS #tasks,
    Projects.projectID AS [Project ID]

FROM Projects  

LEFT JOIN Projects_tasks
ON Projects.projectID = Projects_tasks.projectID 

GROUP BY Projects.projectName,
    Projects.projectID

Comments

0

Try by changing INNER JOIN into LEFT JOIN

SELECT     Projects.projectName AS [Project Name],
       ISNULL(COUNT(Projects_tasks.taskID),0) AS #tasks, Projects.projectID AS [Project ID]

FROM Projects LEFT JOIN Projects_tasks ON Projects.projectID = Projects_tasks.projectID

GROUP BY Projects.projectName,Projects_tasks.taskID,Projects.projectID

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.