Question

I've been given this code that currently ranks Call Centers based on given criteria. I've been tasked with changing this ranking from Center based to Associate Director (AKA AD, Manager, BottomUpLevel02).

The problem is, I don't have a lot of experience working with actually creating tables. I typically manipulate views so I'm sure there are some syntax rules that I'm seriously lacking here.

Here's the code:

SET NOCOUNT ON 
DECLARE @StartDate SMALLDATETIME, 
        @EndDate SMALLDATETIME 
SET @StartDate = '2012/07/01' 
SET @EndDate = '2012/07/31' 

IF OBJECT_ID('tempdb..#ACSResults') IS NOT NULL DROP TABLE #ACSResults 

CREATE TABLE #ACSResults (AreaID VARCHAR(4), Location VARCHAR(50), 
                          MonthName VARCHAR(6),
                          RepResolve FLOAT, ERP FLOAT)
INSERT INTO #ACSResults 
SELECT a.area, a.location,
a.monthname,
CASE WHEN SUM(CASE WHEN a.RepResolve IN ('1','0') 
                        THEN 1 
                   ELSE 0 END) = 0 
          THEN NULL  
          ELSE CAST(SUM(CASE WHEN a.RepResolve = '1' 
                                  THEN 1 
                             ELSE 0 END)AS FLOAT) / CAST(SUM(CASE WHEN a.RepResolve IN ('1','0') 
                                                                       THEN 1 
                                                                  ELSE 0 END) AS FLOAT) END AS REPRESOLVE,
CASE WHEN SUM(CASE WHEN a.ERP IN ('0','1','2','3','4','5','6','7','8', '9', '10') 
                        THEN 1 
                   ELSE 0 END) = 0 
          THEN NULL 
     ELSE (CAST(SUM(CASE WHEN a.ERP IN ('8', '9', '10') 
                              THEN 1 
                         ELSE 0 END) AS FLOAT) / CAST(SUM(CASE WHEN a.ERP in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10') 
                                                                    THEN 1 
                                                               ELSE 0 END) AS FLOAT))END AS ERP

FROM (SELECT CASE WHEN vl.virtuallocationtypeid =13 
                       THEN 'PP'
                  WHEN eh.PeopleSoftDepartmentID IN ('2922', '9050', '9136', '9130','9134','9135', '9137', '9060') 
                       THEN 'FS'
                  WHEN eh.PeopleSoftDepartmentID ='2390' 
                       THEN 'CMO'
                  WHEN eh.virtuallocationtypeid = 3 
                  OR eh.peoplesoftDepartmentid IN ('9165', '9166','9157','9167','<<OSVBSC>>') 
                       THEN 'BSC'
                  WHEN (eh.virtuallocationid IN ('111', '113', '114', '115', '116', '118', '119', '120', '122', '123') 
                        AND acs.OfferDate  < '04/01/2011') 
                  OR (eh.virtuallocationid IN ('116','120','123') 
                      AND acs.OfferDate  >= '04/01/2011') 
                        THEN 'SPC2'
                  WHEN (eh.virtuallocationid IN ('112', '121', '110') 
                        AND acs.OfferDate  >= '07/01/2011') 
                       THEN 'SPC2'
                  WHEN (eh.virtuallocationid IN ('99') 
                        AND acs.OfferDate < '07/01/2011') 
                        AND eh.PeopleSoftDepartmentID IN ('9030', '9080', '9355', '9040', '<<OSCCALTECH>>', '<<WDTSMO>>', '9195','<<OSVTS>>') 
                       THEN 'SPC2'
                  WHEN eh.EmployeeTypeID = 'V' 
                       THEN 'SPC'
                  WHEN vl.virtuallocationtypeid =14 
                       THEN 'SPC2'
                  WHEN (eh.virtuallocationID IS NULL 
                        AND eh.AreaId IS NULL) 
                       THEN eh.PeopleSoftAreaID 
                  WHEN eh.virtuallocationID IS NULL 
                       THEN eh.AreaID 
                       ELSE vl.AreaId END AS Area,
             CASE WHEN eh.VirtualLocationID = 102 
                       THEN 'HQ Quality - GSC'
                  WHEN eh.peoplesoftDepartmentid IN ('9165', '9166','9167') 
                       THEN 'GSC'
                  WHEN eh.peoplesoftdepartmentid ='<<OSVAOL>>' 
                  AND eh.virtuallocationid =64  
                       THEN 'ALORICA AOL'
                  WHEN (eh.virtuallocationid IN ('99', '148')
                        AND eh.PeopleSoftDepartmentID IN ('9030', '9080', '9355', '9040', '<<OSCCALTECH>>', '<<WDTSMO>>', '9195','<<OSVTS>>')) 
                       THEN 'Wichita Falls (WDS) - Tech'
                  WHEN eh.virtuallocationID IS NULL 
                  OR vl.VirtualLocationTypeID = 4 
                       THEN 'Others'
                  ELSE vl.VirtualLocationDescription END AS Location,
             CASE WHEN eh.PeopleSoftDepartmentID IN ( '9180', '<<WEBILING>>','<<OSVSPN>>') 
                       THEN 'Bilingual'
                  WHEN eh.PeopleSoftDepartmentID IN ( '9150', '9440', '9370', '9260', '9330', '9460','9155','9215','9157','9175','<<OSVBSC>>') 
                       THEN 'BSC'
                  WHEN eh.PeopleSoftDepartmentID IN ( '9395', '9010', '9341', '9340', '9450', '9131', '9025', '10014901', '<<SO_WEVIS>>', '<<WE_MWVIS>>', '<<WEVIS>>', '<<NEVIS>>', '<<OSCCALTEL>>', '<<SOVIS>>', '<<NE_MWVIS>>', '<<MWVIS>>', '<<OSVCS>>') 
                       THEN 'Care'
                  WHEN eh.PeopleSoftDepartmentID IN ('9030', '9080', '9355', '9040', '<<OSCCALTECH>>', '<<WDTSMO>>', '9195','<<OSVTS>>' ) 
                       THEN 'Tech'
                  WHEN eh.PeopleSoftDepartmentID IN ('<<AOL_WEVIS>>', '<<OSVAOL>>') 
                       THEN 'AOL'
                  WHEN eh.peoplesoftdepartmentid IN ('<<OSVCS_TCS>>', '<<OSVOB_TCS>>')
                       THEN 'New Hire Transition'
                  WHEN eh.PeopleSoftDepartmentID = '9390' 
                       THEN 'EPP'
                  WHEN eh.PeopleSoftDepartmentID = '9120' 
                       THEN 'ERT'
                  WHEN eh.PeopleSoftDepartmentID = '9190' 
                       THEN 'IRT'
                  WHEN eh.PeopleSoftDepartmentID = '9085' 
                       THEN 'Bilingual Tech'
                  WHEN eh.PeopleSoftDepartmentID IN ('9490', '<<LNP>>','<<OSVLNP>>')
                       THEN 'LNP'
                  WHEN eh.PeopleSoftDepartmentID = '9070' 
                       THEN 'Retention'
                  WHEN eh.PeopleSoftDepartmentID = '9115' 
                       THEN 'GLOBAL'
                  WHEN eh.PeopleSoftDepartmentID IN ('<<ONEBILL>>', '<<OSVOB>>') 
                       THEN 'Onebill'
                  WHEN eh.peoplesoftdepartmentid = '<<PPCARE>>' 
                       THEN 'Prepaid Care'
                  WHEN eh.peoplesoftdepartmentid = '<<PPTECH>>' 
                       THEN 'Prepaid Tech'
                  ELSE 'Other' END AS DeptGroup,
             psd.PeopleSoftDepartmentDescription AS Dept, 
             acs.IVRCallID, 
             acs.OfferDate, 
             acs.EmployeeID,
             acs.ACSSUserID, 
             Cast(acs.DidRepResolve as Float) As RepResolve,
             acs.Question1 AS FCR, 
             acs.Question2 AS ERP, 
             acs.Question3 AS NPS, 
             acs.ACSSCallID,
             month (acs.Offerdate) AS MonthName

      FROM dbCustomerSurvey.Detail.vwAfterCallSurvey acs 
      LEFT JOIN dbEmployee.Summary.vwEmployeeHistory eh 
                ON acs.EmployeeID = eh.EmployeeID 
                AND acs.OfferDate BETWEEN eh.StartDate 
                AND eh.EndDate 
      LEFT JOIN dbEmployee.Config.vwName Rep 
                ON eh.EmployeeID = Rep.EmployeeID 
      LEFT JOIN dbEmployee.Config.vwName Sup 
                ON eh.BottomUp01ID = Sup.EmployeeID
      LEFT JOIN dbEmployee.Config.vwName Mgr 
                ON eh.BottomUp02ID = Mgr.EmployeeID
      LEFT JOIN dbEmployee.Config.vwName Dir 
                ON eh.BottomUp03ID = Dir.EmployeeID
      LEFT JOIN dbEmployee.Config.vwVirtualLocation vl 
                ON eh.VirtualLocationID = vl.VirtualLocationID 
      LEFT JOIN dbEmployee.Config.vwDepartment d 
                ON eh.DepartmentID = d.DepartmentID 
      LEFT JOIN dbEmployee.Config.vwPeopleSoftDepartment psd
                ON eh.PeopleSoftDepartmentID = psd.PeopleSoftDepartmentID
      WHERE acs.CurrentStatus IN ('Completed', 'COMP') 
            AND acs.surveytype IN ('ACS_FCR_Rep_Perform', 'ACS_FCR_Rep_Perform-prepaid', 'ACS_Rep_Perform_BSC2', 'ACS_Rep_Perform_Gov') 
            AND acs.OfferDate BETWEEN @StartDate AND @EndDate) a 

WHERE a.Area IN ('WE', 'mw','ne','so') 
      AND a.Location NOT IN ('West Area Staff - CS', 'Northeast Area Staff - CS', 'Midwest Area Staff - CS', 'Others', 'West Area Finance - CS', 'Midwest Area Marketing, Sales, & Training', 'South Area Staff - CS', 'Midwest Area Finance - CS', 'Bellevue - CS')
      AND a.deptgroup in ('aol','bilingual','Bilingual Tech','care','global','lnp', 'onebill','other','retention','tech', 'new hire transition') 

GROUP BY a.area, a.location, a.monthname

-- Repeat table

IF OBJECT_ID('tempdb..#HRRep')  IS NOT NULL DROP TABLE #HRRep 

CREATE TABLE #HRRep (AreaID VARCHAR(4), Location VARCHAR(50), 
                     MonthName VARCHAR(6), 
                     HourRepeatPercent FLOAT)
INSERT INTO #HRRep (AreaID, Location, MonthName, HourRepeatPercent) 
SELECT eh.AreaID, vl.VirtualLocationDescription, 
       MONTH (acs.statdate) AS MonthName,

       CAST(SUM(acs.Repeats2Hr)as FLOAT) / NULLIF(SUM(acs.Calls2Hr), 0) as 'HourRepeatPercent'

FROM dbReportSummary.ReportSummary.vwRepeatCalls2Hr acs WITH (NOLOCK)
LEFT JOIN dbEmployee.Summary.vwEmployeeHistory eh WITH (NoLOCK) 
          ON acs.EmployeeID = eh.EmployeeID 
          AND acs.StatDate BETWEEN eh.StartDate AND eh.EndDate 
LEFT JOIN dbEmployee.Config.vwVirtualLocation vl 
          ON eh.VirtualLocationID = vl.VirtualLocationID 
LEFT JOIN dbEmployee.Config.vwDepartment d 
          ON eh.DepartmentID = d.DepartmentID 
LEFT JOIN dbEmployee.Config.vwPeopleSoftDepartment psd 
          ON eh.PeopleSoftDepartmentID = psd.PeopleSoftDepartmentID

WHERE acs.StatDate BETWEEN @StartDate AND @EndDate
      AND eh.PeopleSoftDepartmentID IN ('9180', '<<WEBILING>>','<<OSVSPN>>', '9150', '9440', '9370', '9260', '9330', '9460','9155','9215','9157','9175','<<OSVBSC>>', '9395', '9010', '9341', '9340', '9450', '9131', '9025', '10014901', '<<SO_WEVIS>>', '<<WE_MWVIS>>', '<<WEVIS>>', '<<NEVIS>>', '<<OSCCALTEL>>', '<<SOVIS>>', '<<NE_MWVIS>>', '<<MWVIS>>', '<<OSVCS>>', '9030', '9080', '9355', '9040', '<<OSCCALTECH>>', '<<WDTSMO>>', '9195','<<OSVTS>>', '<<AOL_WEVIS>>', '<<OSVAOL>>', '<<OSVCS_TCS>>', '<<OSVOB_TCS>>'    , '9390', '9120', '9190', '9085', '9490', '<<LNP>>', '<<OSVLNP>>', '9070', '9115', '<<ONEBILL>>', '<<OSVOB>>', '<<PPCARE>>', '<<PPTECH>>')
      AND eh.AreaID IN ('MW', 'NE', 'SO', 'WE')
      AND vl.VirtualLocationDescription NOT IN ('West Area Staff - CS', 'Northeast Area Staff - CS', 'Midwest Area Staff - CS', 'Others', 'West Area Finance - CS')
AND eh.Titleid IN ('1','2','3','509') 

GROUPBY  eh.AreaID, vl.VirtualLocationDescription, MONTH (acs.statdate)

-- (?) --

SELECT a.Location, a.RepResolve, a.RRRank, a.ERP, a.ERPRank,a.HourRepeatPercent,
       a.RepeatRank, (a.RRRank + a.ERPRank+a.RepeatRank) as 'Total Rank Points', 
       RANK() OVER(ORDER BY (a.RRRank + a.ERPRank+a.RepeatRank) ASC, a.RepResolve DESC) AS 'Overall Rank'
FROM (SELECT a.Location, a.RepResolve, 
             RANK() OVER(ORDER BY a.RepResolve DESC) AS RRRank, a.ERP, 
             RANK() OVER(ORDER BY a.ERP DESC) AS ERPRank, b.HourRepeatPercent, 
             RANK() OVER(ORDER BY b.HourRepeatPercent ASC) AS RepeatRank
      FROM #ACSResults AS A
      LEFT JOIN #HRRep AS B 
                ON a.areaid = b.areaid 
                AND a.monthname = b.monthname 
                AND a.location = b.location) a
GROUP BY a.Location, a.RepResolve, a.RRRank, a.ERP, a.ERPRank,a.HourRepeatPercent,
         a.RepeatRank, (a.RRRank + a.ERPRank + a.RepeatRank)

I keep getting errors about the select clause not matching the insert clause when I try adding AD to the mix. Can anybody please point out the areas that need to be modified in order to include AD?

EDIT: I was able to successfully add an AD column! The new problem: The results are a bit off. Before when the results were ranked based on location, each distinct location was assigned a rank based on a total of points. It was clean and the numbers added up. Now there are duplicate ADs and the ranking is backwards (least amount of total ranking points is listed as 1st place).

I simply added the AD column to the INSERT statement and the select statements. Can anybody tell me, in addition to adding the AD Column throughout the appropriate locations of the query, should I be modifying any of the operations or functions of the query to reflect a true ranking for the given date range? Please feel free to ask for a clarification on any part of this. Thanks in advance for any assistance you can provide.

Was it helpful?

Solution 2

EDIT: I was able to successfully add an AD column! The new problem: The results are a bit off. Before when the results were ranked based on location, each distinct location was assigned a rank based on a total of points. It was clean and the numbers added up. Now there are duplicate ADs and the ranking is backwards (least amount of total ranking points is listed as 1st place).

I simply added the AD column to the INSERT statement and the select statements. Can anybody tell me, in addition to adding the AD Column throughout the appropriate locations of the query, should I be modifying any of the operations or functions of the query to reflect a true ranking for the given date range? Please feel free to ask for a clarification on any part of this. Thanks in advance for any assistance you can provide.

You'll need AD columns in both your temp tables (#ACS and ##HRRep), and the population of each (i.e. the INSERT INTO.... SELECT FROM) should have the AD column in the GROUP BY. Then your final SELECT will need to join the two tables on the AD column as well as the other columns already in the join expression.

I suspect that you don't need the GROUP BY in the final SELECT, as (I think) the two tables already have the results aggregated by areaid, location etc, and RANK() is a single-row function rather than an aggregate like SUM:

LEFT JOIN #HRRep AS B 
                ON a.areaid = b.areaid 
                AND a.monthname = b.monthname 
                AND a.location = b.location) a
/* Probably don't need this bit
GROUP BY a.Location, a.RepResolve, a.RRRank, a.ERP, a.ERPRank,a.HourRepeatPercent,
         a.RepeatRank, (a.RRRank + a.ERPRank + a.RepeatRank)
But this bit: */
ORDER BY (a.RRRank+a.EPRank+a.RepeatRank) DESC

Laurence's suggestion of creating a mapping table (he's dared to dive into the "hideousness" inside the first set) is great as it isolates the "hideousness" in one place.

OTHER TIPS

To put some flesh on Clockwork Muse's ideas. If you're allowed to create tables, start with (replace the nvarchars with whatever your database actually has)

Create Table dbo.DepartmentGroupLookup (
  PeopleSoftDepartmentID nvarchar(30) Primary Key Not Null,
  DeptGroup nvarchar(30) not null
)

Insert Into dbo.DepartmentGroupLookup (PeopleSoftDepartmentID, DeptGroup) Values
  ('9180', 'Bilingual'), ('<<WEBILING>>', 'Bilingual'), ..., ('9150', 'BSC'), ... etc

Create Function dbo.LookupLocation(
  @VirtualLocationID as int, 
  @VirtualLocationDescription nvarchar(30),
  @VirtualLocationTypeID int,
  @PeopleSoftDepartmentID nvarchar(30)
) Returns nvarchar(30) As
Begin
  Return Case 
    When @VirtualLocationID = 102 Then 'HQ Quality - GSC'
    When @PeopleSoftDepartmentID In ('9165', '9166','9167') Then 'GSC'
    When @PeopleSoftDepartmentID ='<<OSVAOL>>' And @VirtualLocationID = 64 Then 'ALORICA AOL'
    When @VirtualLocationID In ('99', '148') And 
      @PeopleSoftDepartmentID In (
        '9030', '9080', '9355', '9040', '<<OSCCALTECH>>', 
        '<<WDTSMO>>', '9195','<<OSVTS>>'
      ) Then 'Wichita Falls (WDS) - Tech'
    When @VirtualLocationID Is Null Or @VirtualLocationTypeID = 4 Then 'Others'
    Else @VirtualLocationDescription 
    End
 End

Do a similar thing for the Area lookup, now your first inner query becomes

Select
  dbo.LookupArea(eh.virtuallocationID, v1.virtuallocationtypeid, vl.AreaId,  eh.PeopleSoftAreaID,
      eh.PeopleSoftDepartmentID, acs.OfferDate, eh.EmployeeTypeID) As Area, -- There may be other dependencies
  dbo.LookupLocation(eh.VirtualLocationID, vl.VirtualLocationDescription,
      vl.VirtualLocationTypeID, eh.peoplesoftDepartmentid) As Location,
  IsNull(dgl.DeptGroup, 'Other') AS DeptGroup,
  psd.PeopleSoftDepartmentDescription AS Dept, 
  acs.IVRCallID, 
  acs.OfferDate, 
  acs.EmployeeID,
  acs.ACSSUserID, 
  Cast(acs.DidRepResolve as Float) As RepResolve,
  acs.Question1 AS FCR, 
  acs.Question2 AS ERP, 
  acs.Question3 AS NPS, 
  acs.ACSSCallID,
  Month(acs.Offerdate) AS MonthName
From
  dbCustomerSurvey.Detail.vwAfterCallSurvey acs 
    Left Outer Join
  dbEmployee.Summary.vwEmployeeHistory eh 
    On acs.EmployeeID = eh.EmployeeID And acs.OfferDate Between eh.StartDate And eh.EndDate 
    Left Outer Join
  dbEmployee.Config.vwName Rep 
    On eh.EmployeeID = Rep.EmployeeID 
    Left Outer Join 
  dbEmployee.Config.vwName Sup 
    On eh.BottomUp01ID = Sup.EmployeeID
    Left Outer Join
  dbEmployee.Config.vwName Mgr 
    On eh.BottomUp02ID = Mgr.EmployeeID
    Left Outer Join
  dbEmployee.Config.vwName Dir 
    On eh.BottomUp03ID = Dir.EmployeeID
    Left Outer Join
  dbEmployee.Config.vwVirtualLocation vl 
    On eh.VirtualLocationID = vl.VirtualLocationID 
    Left Outer Join
  dbEmployee.Config.vwDepartment d 
    On eh.DepartmentID = d.DepartmentID 
    Left Outer Join
  dbEmployee.Config.vwPeopleSoftDepartment psd
    On eh.PeopleSoftDepartmentID = psd.PeopleSoftDepartmentID
    Left Outer Join
  dbo.DepartmentGroupLookup dgl
    On eh.PeopleSoftDepartmentID = dgl.PeopleSoftDepartmentID
Where 
 acs.CurrentStatus In ('Completed', 'COMP') And
 acs.surveytype In (
     'ACS_FCR_Rep_Perform', 'ACS_FCR_Rep_Perform-prepaid',
     'ACS_Rep_Perform_BSC2', 'ACS_Rep_Perform_Gov'
 ) And
 acs.OfferDate Between @StartDate And @EndDate

The goal is to keep simplifying (or at least moving stuff out of sight) in this way until it's possible to understand what's going on.

Laurence has it right.

Indents are my (and everybody's) friend when SQL code starts looking like this. If a derived set is indented, it's easier to see where its definition starts and ends:

SELECT some columns 
FROM
     (lots of hideous code) SetA
WHERE
    Things AND
    More Things

I've snipped out most of the hideousness from the code-snippet you're unlucky enough to be dealing with:

CREATE TABLE #ACSResults (AreaID VARCHAR(4), Location VARCHAR(50),MonthName VARCHAR(6), RepResolve FLOAT, ERP FLOAT)
INSERT INTO #ACSResults 
  SELECT 
-- ColListStart
a.area,
a.location,
a.monthname,

 CASE WHEN SUM(CASE WHEN a.RepResolve IN ('1','0') THEN 1 ELSE 0 END) = 0 THEN NULL  ELSE CAST(SUM(CASE WHEN a.RepResolve = '1' THEN 1 ELSE 0 END)AS FLOAT) / CAST(SUM(CASE WHEN a.RepResolve IN ('1','0') THEN 1 ELSE 0 END) AS FLOAT) END AS REPRESOLVE,
CASE WHEN SUM(CASE WHEN a.ERP IN ('0','1','2','3','4','5','6','7','8', '9', '10') THEN 1 ELSE 0 END) = 0 THEN NULL ELSE (CAST(SUM(CASE WHEN a.ERP IN ('8', '9', '10') THEN 1 ELSE 0 END) AS FLOAT) / CAST(SUM(CASE WHEN a.ERP in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10') THEN 1 ELSE 0 END) AS FLOAT))END AS ERP
-- ColListEnd
FROM
    (/*Eurgh!  Hideous legacy code that makes my eyes bleed just glancing at it*/) a 

Where  a.Area IN ('WE', 'mw','ne','so') 
and a.Location not in ('West Area Staff - CS', 'Northeast Area Staff - CS', 'Midwest Area Staff - CS', 'Others', 'West Area Finance - CS', 'Midwest Area Marketing, Sales, & Training', 'South Area Staff - CS', 'Midwest Area Finance - CS', 'Bellevue - CS')
and a.deptgroup in ('aol','bilingual','Bilingual Tech','care','global','lnp', 'onebill','other','retention','tech', 'new hire transition') 

group by 
a.area,
a.location,
a.monthname

--------------------------------------------------------------------------------REPEAT TABLE------------------------------------------------------------------------------------------------------
 IF OBJECT_ID('tempdb..#HRRep') 
IS NOT NULL DROP TABLE #HRRep 
CREATE TABLE #HRRep (AreaID VARCHAR(4), Location VARCHAR(50),MonthName VARCHAR(6), HourRepeatPercent FLOAT)
INSERT INTO #HRRep (AreaID, Location, MonthName, HourRepeatPercent) 
  Select
  -- ColList Start
  eh.AreaID, vl.VirtualLocationDescription, Month (acs.statdate) AS MonthName,

Cast(Sum(acs.Repeats2Hr)as float) /nullif(Sum(acs.Calls2Hr), 0) as 'HourRepeatPercent'
    -- COlListEnd
From 
    /* More hideous legacy code */

The "Columns in INSERT and SELECT must match" error you're getting will disappear if you just make sure that the columns in the SELECT match those in the table referenced by the INSERT (my comments -ColListStart and -ColListEnd define this area). So you'd add your extra column to each CREATE TABLE, and then add it (perhaps, initially, just as the literal

'AnAreaDirector' AS AreaDirector

) to the SELECT statement, in the right place in the order of course. You'll nned a GROUP BY AreaDirector at the end of each SELECT (i.e. after everything else) as well.

Then the real fun starts, which is digging into the stuff within the () (which I've snipped out), and figuring out whether you can grab the AD column from the tables referenced within them, or whether you need to join to yet another table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top