查询中的表达式使用PIVOT命令

时间:2014-09-17 20:42:43

标签: sql sql-server-2008 pivot

我正在尝试从包含基于2个不同值的百分比值的表中提取数据 - 触及的记录数/记录总数。我想要每月滚动一次。我有一个似乎在正确的轨道上的查询,但这是一个明显的缺陷,所以我希望得到一些帮助。

以下是查询:

SELECT
    Area_NM,
    [1] AS Jan,
    [2] AS Feb,
    [3] AS Mar,
    [4] AS Apr,
    [5] AS May,
    [6] AS Jun,
    [7] AS Jul,
    [8] AS Aug,
    [9] AS Sep,
    [10] AS Oct,
    [11] AS Nov,
    [12] AS Dec
 FROM
 (select Area_NM, month(Closed_DT) as TMonth, (100. * 

    (
        select COUNT(*) from T_AREA AS DISP
        WHERE P.Area_NM = DISP.Area_NM AND DISP.Branch_MKT = 'NORTHEAST' AND DISP.About <> 'Branch' AND DISP.CAP_TXT IS NOT NULL
    )  
     /  
    (
        select sum(count(*)) over () from T_AREA AS TOT
        WHERE P.Area_NM = TOT.Area_NM AND TOT.Branch_MKT = 'NORTHEAST' AND TOT.About <> 'Branch' 
    )) as perc
    from T_AREA AS P
    WHERE P.Branch_MKT = 'NORTHEAST' AND P.About <> 'Branch'

    ) source    
PIVOT
(    
SUM(perc) **OBVIOUS ERROR
    FOR TMonth
    IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] )
   ) AS pvtMonth

结果应如下所示:

Area Name | Jan | Feb | Mar | Apr | ......
____________________________________________
Arlington    44%   0%    0%    0%......

(其中阿灵顿从TOT获得9分,在1月份从DISP获得4分,在剩下的几个月中在DISP中获得0分) 实际结果是数字,而不是准确的百分比。

错误似乎与调用PIVOT命令的方式有关。因为我已经运行了表达式,所以从技术上讲我不需要SUM,我只需要表达式的值。正确的吗?

编辑: 添加一些样本数据:

CREATE TABLE [T_AREA](
[Case_NBR] [varchar](18) NULL,
[About] [varchar](15) NULL,
[CAP_TXT] [varchar](50) NULL,
[Complaint_TYP] [varchar](11) NULL,
[Branch_MKT] [varchar](9) NULL,
[Branch_DIV] [varchar](30) NULL,
[Area_NM] [varchar](30) NULL,
[Branch_NM] [varchar](30) NULL,
[Closed_DT] [datetime] NULL

) ON [PRIMARY]

GO


INSERT INTO [T_AREA]
[Case_NBR],[About],[CAP_TXT],[UPDT_DT],[Complaint_TYP],[Branch_MKT],[Branch_DIV], [Area_NM],[Branch_NM],[Closed_DT])
VALUES
('05/21/2014-1577309','Branch Teammate',NULL,'Operational','NORTHEAST','Gr Washington and Maryland','DC N Arlington GWM Area','1275 K STREET','5/23/2014  11:44:34 AM'),
('05/21/2014-1577382','Branch Teammate','No Action','Service','NORTHEAST','North and South Carolina','Hickory Salisbury NCSC Area','STATESVILLE BOULEVARD','5/27/14 3:05 PM'),
('05/21/2014-1577949','Branch Teammate',NULL,'Operational','NORTHEAST','Total Virginia','Richmond E Henrico VA Area','TENTH AND MAIN','5/21/14 6:41 PM'),
('05/22/2014-1578740','Branch Teammate','No Action','Service','NORTHEAST','Gr Washington and Maryland','Baltimore Harford GWM Area','WATERVIEW TOWN CENTER','5/22/14 8:58 AM'),
('05/22/2014-1579248','Branch Teammate','No Action','Operational','NORTHEAST','North and South Carolina','Winston Salem NCSC Area','MEDICAL PARK','4/27/14 9:03 AM'),
('05/22/2014-1579250','Branch Teammate','No Action','Service','NORTHEAST','Total Tennessee','Nashville Rutherford TN Area','MURFREESBORO ROAD','4/22/14 5:04 PM'),
('05/22/2014-1579578','Branch Teammate','No Action','Operational','NORTHEAST','Gr Washington and Maryland','Alexandria S Arling GWM Area','LANDMARK','5/22/14 6:11 PM'),
('05/22/2014-1579605','Branch Teammate',NULL,'Operational','NORTHEAST','Gr Washington and Maryland','Baltimore Harford GWM Area','NORTH CHARLES','5/27/14 10:36 AM'),
('05/22/2014-1579609','Branch Teammate',NULL,'Operational','NORTHEAST','Gr Washington and Maryland','North PG GWM Area','BLADENSBURG','6/3/14 9:25 PM'),
('05/23/2014-1580473','Branch Teammate','No Action','Operational','NORTHEAST','Gr Washington and Maryland','Fairfax Prince Wm GWM Area','MONTCLAIR','6/29/14 5:12 PM'),
('05/23/2014-1580518','Branch Teammate',NULL,'Service','NORTHEAST','Gr Washington and Maryland','S Montgomery GWM Area','OLNEY','6/23/14 10:42 AM'),
('05/23/2014-1580667','Branch Teammate','No Action','Operational','NORTHEAST','North and South Carolina','Greater S Charlotte NCSC Area','HILLTOP-MONROE','5/29/14 10:09 AM'),
('05/23/2014-1580687','Branch Teammate',NULL,'Operational','NORTHEAST','Gr Washington and Maryland','North PG GWM Area','ARDWICK-ARDMORE','3/23/14 12:18 PM'),
('05/23/2014-1580784','Branch Teammate',NULL,'Operational','NORTHEAST','North and South Carolina','Hickory Salisbury NCSC Area','STATESVILLE BOULEVARD','6/28/14 3:28 PM'),
('05/23/2014-1581167','Branch Teammate','No Action','Operational','NORTHEAST','Total Tennessee','Memphis TN Area','POPLAR & HIGHLAND','5/29/14 10:38 AM');

1 个答案:

答案 0 :(得分:0)

经过多次麻烦拍摄我发现了错误,这是一些错误。我将在下面列出: 1.主查询中需要DISTINCT 2.为主查询添加了一个额外的过滤器,将结果降低到相同的级别 3.缺少子查询中的比较回到月份列

以下是最终查询:

    SELECT
    Area_NM,
    [1] AS Jan,
    [2] AS Feb,
    [3] AS Mar,
    [4] AS Apr,
    [5] AS May,
    [6] AS Jun,
    [7] AS Jul,
    [8] AS Aug,
    [9] AS Sep,
    [10] AS Oct,
    [11] AS Nov,
    [12] AS Dec
FROM
(select DISTINCT Area_NM, month(Closed_DT) as TMonth, (100 * 

    (
        select COUNT(*) from T_AREA AS DISP
        WHERE P.Area_NM = DISP.Area_NM AND month(P.Closed_DT) = month(DISP.Closed_DT) AND DISP.Branch_MKT = 'NORTHEAST' AND DISP.About <> 'Branch' AND DISP.CAP_TXT IS NOT NULL
    )  
     /  
    (  
        select sum(count(*)) over () from T_AREA AS TOT
        WHERE P.Area_NM = TOT.Area_NM AND month(P.Closed_DT) = month(TOT.Closed_DT) AND TOT.Branch_MKT = 'NORTHEAST' AND TOT.About <> 'Branch'
        ) 

     ) as perc
    from T_AREA AS P
    WHERE P.Branch_MKT = 'NORTHEAST' AND P.About <> 'Branch' AND P.CAP_TXT IS NOT NULL

    ) source
PIVOT
(
    SUM(perc)
    FOR TMonth
    IN ( [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12] )
) AS pvtMonth