Solved

Left Outer Join of Multiple Tables

Posted on 2008-06-24
6
1,034 Views
Last Modified: 2012-08-13
With the below stored procedure, we are attempting to return a sum of "Units" from "Schedule" and a sum of the units that were delivered from the "VisitLog & VisitDetail" tables.  The problem is that with the below code, if there is no visit that matches the "Schedule" it will not include the row(s).  This obviously produces incorrect results, and we are needing help creating a left outer join between these tables.


ALTER  PROCEDURE ReportRunSummary
(
@DateStart DateTime,
@DateEnd DateTime,
@DCN int,
@CountiesID int
)
 AS

SELECT DCN, ClientName,
monthlyauthunits as MonthlyAuthUnits, ScheduleExports.ServiceCode,
sum (NumberOfUnits /4.0) As DeliveredUnits, sum(ScheduleExports.units) - sum (NumberOfUnits /4.0) As [Difference], sum(ScheduleExports.units) As ScheduledUnits
FROM scheduleexports, visitlog, VisitDetail, Services
WHERE
visitDate >= @DateStart
and
visitDate <= @DateEnd
and
PatientID = DCN
and
(DCN = @DCN or @DCN = -1)
And
Visit_Date >= @DateStart
And
Visit_Date <= @DateEnd
And
visitDate = Visit_Date
And
VisitDetail.VisitID = VisitLog.VisitId
And
Convert(int,VisitDetail.ServiceCode) = Services.ServicesID
And
ServiceAbbr = ScheduleExports.ServiceCode
and
(County = @CountiesID or @CountiesID =-2)

group by ScheduleExports.servicecode, DCN, ClientName, monthlyauthunits
order by ClientName, ScheduleExports.ServiceCode
0
Comment
Question by:Element74
  • 3
  • 3
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
please change the query so that each column name is prefixed with the table name is comes from.
otherwise it will be impossible to know where it comes from, and to change the query to the JOIN  or LEFT JOIN sytnax.
0
 

Author Comment

by:Element74
Comment Utility

ALTER  PROCEDURE ReportRunSummary
(
@DateStart DateTime,
@DateEnd DateTime,
@DCN int,
@CountiesID int
)
 AS

SELECT ScheduleExports.DCN, ScheduleExports.ClientName, ScheduleExports.monthlyauthunits as MonthlyAuthUnits, ScheduleExports.ServiceCode, sum (VisitLog.NumberOfUnits /4.0) As DeliveredUnits, sum(ScheduleExports.units) - sum (VisitLog.NumberOfUnits /4.0) As [Difference], sum(ScheduleExports.units) As ScheduledUnits
FROM scheduleexports, visitlog, VisitDetail, Services
WHERE
ScheduleExports.visitDate >= @DateStart
and
ScheduleExports.visitDate <= @DateEnd
and
VisitLog.PatientID = ScheduleExports.DCN
and
(ScheduleExports.DCN = @DCN or @DCN = -1)
And
VisitLog.Visit_Date >= @DateStart
And
VisitLog.Visit_Date <= @DateEnd
And
ScheduleExports.visitDate = VisitLog.Visit_Date
And
VisitDetail.VisitID = VisitLog.VisitId
And
Convert(int,VisitDetail.ServiceCode) = Services.ServicesID
And
Services.ServiceAbbr = ScheduleExports.ServiceCode
and
(ScheduleExports.County = @CountiesID or @CountiesID =-2)

group by ScheduleExports.servicecode, ScheduleExports.DCN, ScheduleExports.ClientName, ScheduleExports.monthlyauthunits
order by ScheduleExports.ClientName, ScheduleExports.ServiceCode
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
good !
now, step 1: use the JOIN syntax (and table aliases).

tell, me, won't that be much easier to read?
ALTER  PROCEDURE ReportRunSummary

(

@DateStart DateTime,

@DateEnd DateTime,

@DCN int,

@CountiesID int

)

 AS

 

SELECT se.DCN

, se.ClientName

, se.monthlyauthunits as MonthlyAuthUnits

, se.ServiceCode

, sum (vl.NumberOfUnits /4.0) As DeliveredUnits

, sum(se.units) - sum (vl.NumberOfUnits /4.0) As [Difference]

, sum(se.units) As ScheduledUnits

FROM scheduleexports se

JOIN visitlog vl

  ON vl.PatientID = se.DCN

 AND vl.Visit_Date = se.visitDate 

 AND vl.Visit_Date >= @DateStart

 AND vl.Visit_Date <= @DateEnd

JOIN VisitDetail vd

  ON vd.VisitID = vl.VisitId

JOIN Services s

  ON s.ServicesID = Convert(int , vd.ServiceCode)

 AND s.ServiceAbbr = se.ServiceCode

WHERE se.visitDate >= @DateStart

  AND se.visitDate <= @DateEnd

  AND ( se.DCN = @DCN or @DCN = -1)

  AND ( se.County = @CountiesID or @CountiesID =-2 )

 

GROUP BY se.servicecode

  , se.DCN

  , se.ClientName

  , se.monthlyauthunits

ORDER BY se.ClientName

  , se.ServiceCode

Open in new window

0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
proceeding to step 2: return rows even if in visitlog/visitdetail there are no records matching.

huh, that was easy? :)
ALTER  PROCEDURE ReportRunSummary

(

@DateStart DateTime,

@DateEnd DateTime,

@DCN int,

@CountiesID int

)

 AS

 

SELECT se.DCN

, se.ClientName

, se.monthlyauthunits as MonthlyAuthUnits

, se.ServiceCode

, sum (vl.NumberOfUnits /4.0) As DeliveredUnits

, sum(se.units) - sum (vl.NumberOfUnits /4.0) As [Difference]

, sum(se.units) As ScheduledUnits

FROM scheduleexports se

LEFT JOIN visitlog vl

  ON vl.PatientID = se.DCN

 AND vl.Visit_Date = se.visitDate 

 AND vl.Visit_Date >= @DateStart

 AND vl.Visit_Date <= @DateEnd

LEFT JOIN VisitDetail vd

  ON vd.VisitID = vl.VisitId

LEFT JOIN Services s

  ON s.ServicesID = Convert(int , vd.ServiceCode)

 AND s.ServiceAbbr = se.ServiceCode

WHERE se.visitDate >= @DateStart

  AND se.visitDate <= @DateEnd

  AND ( se.DCN = @DCN or @DCN = -1)

  AND ( se.County = @CountiesID or @CountiesID =-2 )

 

GROUP BY se.servicecode

  , se.DCN

  , se.ClientName

  , se.monthlyauthunits

ORDER BY se.ClientName

  , se.ServiceCode

Open in new window

0
 

Author Comment

by:Element74
Comment Utility
Thanks for the quick responses. We ran the query this way and the numbers are too high. I suspect some rows are being pulled in from the joined table that we don't need.

I.e. the services table is a straight join between the schedule table, the service table, and the visit log table. What it does is allows us to join the schedule table to the visit log on the service code.

The schedule table uses a character abbreviation in its service code, but the visit log uses a number code, so the join table links this. I think the left join may be matching too many rows between those tables.

The same thing may be true for the visitlog and visitdetail tables. They are a one to many relationship. Each visit may have multiple visitdetails, but each visitdetail only has one visit.

I apologize, but I am really unfamiliar with outer joins and I am not really a database person, so I hope my questions don't sound too stupid.

Do you have any idea if we are on the right track towards fixing this? What other information will help answer our question?

Thanks
0
 

Accepted Solution

by:
Element74 earned 0 total points
Comment Utility
Here is the solution that we finally came up with.  This query, along with setting up multi-column indexes, improved the runtime of our webform from over 2 minutes to around 5 - 6 seconds.

DECLARE @DateStart datetime
DECLARE @DateEnd datetime
DECLARE @DCN int
DECLARE @FilterMode int
Declare @COuntiesID int
 
Select @CountiesID = -2
 
SELECT @DateStart = '5/1/2008'
SELECT @DateEnd = '5/31/2008'
--SELECT @DCN = 62530682
--SELECT @DCN = 58299465
SELECT @DCN = -1
 
-- @FilterMode
--   1 = All For Date Range
--   2 = More Than Necessary Units
--   3 = Less Than Necessary Units
--   4 = No Units At All
 
SELECT @FilterMode = 3
 
SELECT v.*
  FROM (SELECT se_i.DCN,
               se_i.ClientName,
               se_i.monthlyauthunits as MonthlyAuthUnits,
               se_i.ServiceCode,
               se_i.VisitStart,
               se_i.VisitEnd,
               se_i.units,
               (ISNULL ((SELECT sum (vl.NumberOfUnits)
                           FROM visitlog vl,
                                visitdetail vd,
                                services s
                           WHERE s.ServicesID = Convert(int , vd.ServiceCode)
                             AND s.ServiceAbbr = se_i.ServiceCode        
                             AND vd.VisitID = vl.VisitId
                             AND vl.PatientID = se_i.DCN
                             AND vl.Visit_date = se_i.VisitDate
                                                     ), 0) / 4.0) As DeliveredUnits
          FROM scheduleexports se_i
          WHERE se_i.visitDate >= @DateStart
            AND se_i.visitDate <= @DateEnd
            AND (@DCN = -1 or se_i.DCN = @DCN )
            AND (@CountiesID =-2 or se_i.County = @CountiesID)) as v
   WHERE ((@FilterMode = 1)
             OR (@FilterMode = 2 AND v.DeliveredUnits > v.units)
             OR (@FilterMode = 3 AND v.DeliveredUnits < v.units)
             OR (@FilterMode = 4 AND v.DeliveredUnits = 0))
   ORDER BY v.ClientName, v.ServiceCode
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 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

11 Experts available now in Live!

Get 1:1 Help Now