Query using SubQuery returns nothing

I have the below query/subquery.  The subquery by itself does return data so I know that is working.  However, I simply want a column with the next change, yet when I run it, no rows are returned, just the message Command(s) completed successfully!  But here are my rows?

SELECT  Total_2004_SQ-Total_2003_SQ AS NetChange
      From (SELECT s.BusinessEntityID, SUM(s2004.SalesQuota) AS Total_2004_SQ,SUM(s2003.SalesQuota) As Total_2003_SQ
      FROM Sales.SalesPerson As s
      LEFT OUTER JOIN Sales.SalesPersonQuotaHistory AS s2004
            ON s.BusinessEntityID = s2004.BusinessEntityID AND YEAR(s2004.QuotaDate) = 2004
      LEFT OUTER JOIN Sales.SalesPersonQuotaHistory As s2003
            ON s.BusinessEntityID = s2003.BusinessEntityID AND YEAR(s2003.QuotaDate) = 2003
      GROUP BY s.BusinessEntityID)

This is on a SQL Server 2008 database.

Sandra
Sandra SmithRetiredAsked:
Who is Participating?
 
dsackerContract ERP Admin/ConsultantCommented:
Right after you close the subquery with an ending paranthesis, give it an alias.

GROUP BY s.BusinessEntityID)  AS myResult

Open in new window

0
 
dsackerContract ERP Admin/ConsultantCommented:
If even one of the left joins are null, your calculation will be null.

Replace your top line as follows:
SELECT  ISNULL(Total_2004_SQ, 0) - ISNULL(Total_2003_SQ, 0) AS NetChange

Open in new window

0
 
Sandra SmithRetiredAuthor Commented:
Before I tried your suggestion, I re-ran the query and now I get the message "Incorrect syntax near ')' at line 10, which is the line

      LEFT OUTER JOIN Sales.SalesPersonQuotaHistory As s2003

this is in the subquery, which does return results by itself so now I am in a quandary.  I made your suggested changes and still get this same error.

Sandra
0
 
Sandra SmithRetiredAuthor Commented:
That was it!  So, I need to remember to alias my subqueries.  Thank you.

Sandra
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.