包括经理层次结构

时间:2012-11-15 18:04:12

标签: sql sql-server hierarchy peoplesoft

我已经获得了此代码,该代码目前根据给定的条件对呼叫中心进行排名。我的任务是将此排名从中心更改为副主任(AKA AD,经理,BottomUpLevel02)。

问题是,我没有很多使用实际创建表的经验。我通常操纵视图,所以我确信有一些我在这里严重缺乏的语法规则。

以下是代码:

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

- 重复表

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)

当我尝试将AD添加到混合中时,我一直收到有关select子句与insert子句不匹配的错误。任何人都可以指出需要修改的区域以包含AD吗?

编辑:我能够成功添加AD列!新问题:结果有点偏。在基于位置对结果进行排名之前,基于总点数为每个不同位置分配排名。很干净,数字加起来。现在有重复的AD并且排名是倒退的(总排名点的最少量被列为第一位)。

我只是将AD列添加到INSERT语句和select语句中。任何人都可以告诉我,除了在查询的适当位置添加AD列之外,我是否应该修改查询的任何操作或功能以反映给定日期范围的真实排名?请随时要求澄清这方面的任何部分。提前感谢您提供的任何帮助。

3 个答案:

答案 0 :(得分:1)

把一些肉体放在发条缪斯的想法上。如果您被允许创建表,请从(用您的数据库实际拥有的任何内容替换nvarchars)

开始
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

为区域查找做类似的事情,现在你的第一个内部查询变为

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

目标是以这种方式继续简化(或至少移动东西),直到可以理解正在发生的事情为止。

答案 1 :(得分:0)

劳伦斯做得对。

当SQL代码开始看起来像这样时,缩进是我(和每个人的)朋友。如果派生集缩进,则更容易看到其定义的开始和结束位置:

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

我已经从你不幸遇到的代码片段中删除了大部分可怕的内容:

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 */

如果只确保SELECT中的列与INSERT引用的表中的列匹配,那么“INSERT和SELECT中的列必须匹配”错误将会消失(我的注释-ColListStart和-ColListEnd定义了这个区域)。所以你要在每个CREATE TABLE中添加你的额外列,然后添加它(也许,最初,就像文字一样

'AnAreaDirector' AS AreaDirector

)到SELECT语句,按顺序在正确的位置。你也会在每个SELECT的末尾(即其他所有的东西之后)找到一个GROUP BY AreaDirector。

然后开始真正的乐趣,即挖掘()中的内容(我已经剪掉了),并确定是否可以从其中引用的表中获取AD列,或者是否需要加入另一张桌子。

答案 2 :(得分:0)

  

编辑:我能够成功添加AD列!新问题:   结果有点偏。在结果排名之前   在位置上,每个不同的位置都被分配了一个基于a的排名   总分。很干净,数字加起来。现在有   重复AD并且排名是倒退的(最少总数   排名积分列为第一名。

     

我只是将AD列添加到INSERT语句和select中   声明。任何人都可以告诉我,除了添加AD列   我应该在查询的适当位置   修改查询的任何操作或功能以反映a   给定日期范围的真实排名?请随时索取   澄清这方面的任何部分。提前感谢任何人   你可以提供的帮助。

您需要在临时表(#ACS和## HRRep)中使用AD列,并且每个列的总体(即INSERT INTO .... SELECT FROM)应该在GROUP BY中具有AD列。 然后,您的最终SELECT将需要连接AD列上的两个表以及连接表达式中已有的其他列。

我怀疑你在最终的SELECT中不需要GROUP BY,因为(我认为)这两个表已经有了由areaid,location等聚合的结果,而RANK()是单行函数而不是像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建议制作一张地图表(他敢于潜入第一套内部的“丑陋”),这很好,因为它将“丑陋”隔离在一个地方。