Solved

Left Outer Join of Multiple Tables

Posted on 2008-06-24
6
1,043 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]
ID: 21859909
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
ID: 21860015

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]
ID: 21860100
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21860117
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
ID: 21860345
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
ID: 22016042
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

920 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

18 Experts available now in Live!

Get 1:1 Help Now