0

I'm trying to creating a query where I return a property name and a total of work orders opened for each property. However, I'm getting always null for the TotalWorkOrders even though I have work orders for the properties.

My query:

SELECT p.PropertyName, p.PropertyID,
   TotalWorkOrders
  FROM Properties p
  LEFT JOIN (
        Select
          PropertyID,
          SUM(1) AS TotalWorkOrders
        From WorkOrders
       Where WorkOrderComplete=1
       ) sums ON sums.PropertyID = p.PropertyID
 Where p.PropertyID in (Select PropertyID From Properties Where CompanyID = 290)

Does anybody know what I'm doing wrong?

Thanks

2
  • 1
    No GROUP BY in the subquery? Commented Feb 8, 2018 at 15:59
  • Thanks... A group by fixed the problem Commented Feb 8, 2018 at 15:59

1 Answer 1

1

You should probably add a GROUP BY inside the "sums" sub-query.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.