How can I get this SQL count query to return 0 count results?

Here is the query example:

strSQL = "SELECT substring(empno, 2, LEN(empno)),
                 substring(email, 1, LEN(email)-15),
                 IsNull(COUNT(DISTINCT recid),0) as thecount
          from reldocnotes
          left outer join techstaff ON reldocnotes.enteredby = substring(email, 1, LEN(email)-15)
          where techstaff.active <> '0' AND
                substring(techstaff.empno, 2, LEN(techstaff.empno)) <> '127' AND
                thedate between '"&startDate&"' AND '"&endDate&"'
          group by substring(techstaff.empno, 2, LEN(techstaff.empno)), substring(techstaff.email, 1, (LEN(techstaff.email)-15))
          order by thecount " & sortonorder & ";"

Open in new window


Can anyone point me in the right direction? Thanks!
Jordan JohnsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
Your issue is that you are left joining to techstaff then placing numerous restrictions on it in your WHERE clause which invalidate the LEFT JOIN in effect making it an INNER JOIN.  Move the techstaff-specific portions to the JOIN clause.  Also, the COUNT function will only count NON-NULL values so if you COUNT the primary key of techstaff for instance you will get zeros for all reldocnotes records that have no match in techstaff.
1
Vitor MontalvãoMSSQL Senior EngineerCommented:
As stated above COUNT only counts NOT NULL values so the ISNULL function there is useless. Try to use it on recid instead:
strSQL = "SELECT substring(empno, 2, LEN(empno)),
                 substring(email, 1, LEN(email)-15),
                 COUNT(DISTINCT IsNull(recid,0)) as thecount
          from reldocnotes
          left outer join techstaff ON reldocnotes.enteredby = substring(email, 1, LEN(email)-15)
          where techstaff.active <> '0' AND
                substring(techstaff.empno, 2, LEN(techstaff.empno)) <> '127' AND
                thedate between '"&startDate&"' AND '"&endDate&"'
          group by substring(techstaff.empno, 2, LEN(techstaff.empno)), substring(techstaff.email, 1, (LEN(techstaff.email)-15))
          order by thecount " & sortonorder & ";"

Open in new window

1
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I'm not sure we understand the desired result. recid is located in reldocnotes, I suppose? And you want to count distinct recid, which might be 0 to x then because recid might contain NULL? Since reldocnotes is the "left outer join" table, you will never have "no count", as long as recid has values different from NULL.  Records have to exist in reldocnotes for empno and email, otherwise you won't see a row.
And of course what Brain told about the WHERE and outer joins is correct - this is no outer join anymore.

I've the suspicion that you want it reversed. techstaff is the main table, and you want to get the count of (unique) release document notes in reldocnotes for each of the stuff.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

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.