Link to home
Start Free TrialLog in
Avatar of javierpdx
javierpdxFlag for United States of America

asked on

TSQL joins

I'm trying to setup this query so that all records form the Registration table show and when G.MYID does not = R.MYID, then the value would be a zero.  


SELECT        COUNT(G.GuestRegID) AS GCOUNT, R.MYID
FROM            dbo.Guest AS G Full OUTER JOIN
                         dbo.Registration AS R ON G.MYID= R.MYID
WHERE        (G.IsActive = 1)
GROUP BY G.MYID, R.MYID

How could I modify this query to show all records in R, and if G.MYID does not equal R.MYID then return a zero?

I'm using SQL Server 2012.
Thanks.
Avatar of sameer2010
sameer2010
Flag of India image

Hi, if you want to show all records in R and just show 0 as the count for rows that do not have G row, you can do the following
SELECT        R.MYID, isnull(g.myid,0) growexsists
FROM            dbo.Registration AS R left outer join dbo.Guest AS G ON G.MYID= R.MYID
WHERE        (G.IsActive = 1)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of javierpdx

ASKER

Thanks.  I was struggling with this.