Solved

How to get a record with a COUNT to be returned from a JOIN, even when the COUNT is 0 (zero)?

Posted on 2007-03-26
5
194 Views
Last Modified: 2010-03-19
SQL Server 2005

How to get a record with a COUNT to be returned from a JOIN, even when the COUNT is 0 (zero)?

The following stored proc returns records when the COUNT is greater than zero. I know the SQL is doing exactly what I'm asking it to do - find records that meet the criteria through the JOIN. But, I'm brain farting on how to return the record even when the COUNT is zero. Do I need to use a subquery?==========================================================
CREATE PROCEDURE [dbo].[GetListOfSessionsWithCountByUserId]
@paramUserID int
AS
BEGIN
SELECT  dbo.tblSession.pk_lngSessionID,
              dbo.tblSession.fk_lngUserID,
              dbo.tblSession.dtmSessionDate,
              dbo.tblSession.strSessionClientName,  
              COUNT(dbo.tblSessionDetail.intActive) AS TotalActive

 FROM   dbo.tblSession
 INNER JOIN  dbo.tblSessionDetail
   ON dbo.tblSession.pk_lngSessionID = bo.tblSessionDetail.pk_lngSessionID
WHERE     (dbo.tblSession.fk_lngUserID = @paramUserID) AND (dbo.tblSessionDetail.intActive = 1)
GROUP BY dbo.tblSession.pk_lngSessionID, dbo.tblSession.fk_lngUserID, dbo.tblSession.dtmSessionDate, dbo.tblSession.strSessionClientName
END
==============================================
What's the right way to get ALL records, with a count that may be zero or greater?

Thanks!

Dennis

0
Comment
Question by:dtleahy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 29

Expert Comment

by:Nightman
ID: 18796115
Use a LEFT JOIN instead of an INNER JOIN
0
 

Author Comment

by:dtleahy
ID: 18796163
LEFT JOIN produces the same results
0
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 18796188
change to this:

SELECT  dbo.tblSession.pk_lngSessionID,
              dbo.tblSession.fk_lngUserID,
              dbo.tblSession.dtmSessionDate,
              dbo.tblSession.strSessionClientName,  
              COUNT(dbo.tblSessionDetail.intActive) AS TotalActive

 FROM   dbo.tblSession
 LEFT JOIN  dbo.tblSessionDetail
   ON dbo.tblSession.pk_lngSessionID = bo.tblSessionDetail.pk_lngSessionID AND dbo.tblSessionDetail.intActive = 1
WHERE  dbo.tblSession.fk_lngUserID = @paramUserID
GROUP BY dbo.tblSession.pk_lngSessionID, dbo.tblSession.fk_lngUserID, dbo.tblSession.dtmSessionDate, dbo.tblSession.strSessionClientName

I have simply moved the dbo.tblSessionDetail.intActive =1 condition to the join instead of the WHERE clause.
0
 

Author Comment

by:dtleahy
ID: 18796225
Cha ching! Excellent! Nightman, thank you very much for sharing your expertise!

Sometimes, I really feel like a SQL beginner. I would not have come up with that elegant solution - I'm sure I could have eventually done something using a nested subquery that first got all the rows for that user ID, but your solution is... elegant!

Dennis
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18796238
Dennis, it's an absolute pleasure. Good luck with your project!
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question