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
184 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now