We help IT Professionals succeed at work.
Get Started

LEFT JOIN when using CROSS  APPLY

al4629740
al4629740 asked
on
101 Views
Last Modified: 2015-03-03
I have an SQL query which is delivering a LEFT JOIN list of groups from my tblOrgProfile table.

DECLARE @fromdt AS datetime 
                DECLARE @todt AS datetime 
                SET @fromdt = '12/1/2014' 
                SET @todt = '12/31/2014'
                SELECT  p.Agency, count(distinct h.RegID) 
                FROM tblOrgProfile as p 
                LEFT JOIN tblOrgRegistrations as r 
                    ON p.AgencyID = r.AgencyID 
                    AND r.Volunteer = 1 
                LEFT JOIN tblOrgHours as h 
                    ON h.RegID = r.regid 
                    AND h.ActivityDate >= @fromdt 
                    AND h.ActivityDate <= @todt 
                    AND h.Hours > 0 
                    AND h.VolunteerHour = 1
                    WHERE p.CSWGroup = 1 
                GROUP BY p.Agency 
                ORDER BY p.Agency

Open in new window


I want the same LEFT JOIN list of groups to pop up in the following query, but am unable.  Is there a way to LEFT JOIN the tblOrgProfile table in the second query below?

SELECT P.Agency, CA.LinkageName FROM tblOrgProfile P CROSS APPLY (SELECT 
                              STUFF((SELECT '; ' + L.LinkageName 
                                    FROM tblOrgMonthlyReport M 
                                    JOIN tblOrgMonthlyLinkages L ON M.MonthlyID = L.MonthlyID 
                                    Where P.AgencyID = m.AgencyID 
                                    FOR XML PATH ('')) 
                                 , 1, 1, '') 
                             , STUFF((SELECT '; ' + L.Linkage 
                                    FROM tblOrgMonthlyReport M 
                                    JOIN tblOrgMonthlyLinkages L ON M.MonthlyID = L.MonthlyID 
                                    Where P.AgencyID = m.AgencyID 
                                    FOR XML PATH ('')), 1, 1, '')) CA (LinkageName, Linkage) where P.CSWGroup = 1

Open in new window

Comment
Watch Question
Database and Application Developer
Commented:
This problem has been solved!
Unlock 2 Answers and 7 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE