为什么我的数据透视查询没有正确分组?

时间:2019-03-01 07:30:18

标签: sql-server tsql pivot

我正在使用SQL Server 2014,并且有以下Pivot查询。我的问题是查询未正确分组。实际的T-SQL查询及其输出如下所示:

SELECT [Market], [Actual], [Budget]

FROM

(

select distinct Market, 
       sum(rn) over (partition by Market) as [RN], 
       sum(rn) over () as [Total],
       cast(CAST(((100.0 * (sum(rn) over (partition by Market))/(sum(rn) over ()))) AS DECIMAL(19,1)) as varchar(10))+ ' ' + '%'  as [Percentage],
       'Actual' as [Type]

from [View1]


UNION ALL

select distinct Market, 
       sum(rn) over (partition by market) as [RN], 
       sum(rn) over () as Total,
       cast(CAST(((100.0 * (sum(rn) over (partition by Market))/(sum(rn) over ()))) AS DECIMAL(19,1)) as varchar(10))+ ' ' + '%'  as [Percentage],
       'Budget' as [Type]
from [Budget1]


)xx

PIVOT

(

MIN([Percentage])

FOR [Type] IN ([Actual], [Budget])


) AS pvt_table

我的源数据的摘要(即上面的内部查询):

Market           RN     Total   Percentage    Type
Belgium          240    5337    4.5 %         Budget
Belgium          213    5191    4.1 %         Actual
Central Europe    35    5337    0.7 %         Budget
Central Europe   100    5191    1.9 %         Actual

从运行整个查询中提取的当前输出:

Market           Actual    Budget
Belgium          4.1 %     NULL
Belgium          NULL      4.5 %
Central Europe   1.9 %     NULL
Central Europe   NULL      0.7 %

我期望得到的结果:

Market           Actual    Budget
Belgium          4.1 %     4.5 %
Central Europe   1.9 %     0.7 %

我期望按市场分组在我的Pivot查询中是明确的。

2 个答案:

答案 0 :(得分:2)

尝试这样使用

create TABLE #Table1
    ([Market] varchar(14), [RN] int, [Total] int, [Percentage] varchar(6), [Type] varchar(6))
;

INSERT INTO #Table1
    ([Market], [RN], [Total], [Percentage], [Type])
VALUES
    ('Belgium', 240, 5337,        ' 4.5 % ', 'Budget'),
    ('Belgium', 213, 5191,        ' 4.1 % ', 'Actual'),
    ('Central Europe', 35, 5337,  ' 0.7 % ', 'Budget'),
    ('Central Europe', 100, 5191, ' 1.9 % ', 'Actual')
;
 select market,max([Actual])[Actual] ,max([Budget])[Budget] from #Table1
PIVOT (MAX([Percentage]) 
       FOR type in ([Actual],[Budget])) AS pvt
       group by market 

SELECT [Market], max([Actual])Actual , max([Budget])Budget

FROM

(

select distinct Market, 
       sum(rn) over (partition by Market) as [RN], 
       sum(rn) over () as [Total],
       cast(CAST(((100.0 * (sum(rn) over (partition by Market))/(sum(rn) over ()))) AS DECIMAL(19,1)) as varchar(10))+ ' ' + '%'  as [Percentage],
       'Actual' as [Type]

from [View1]


UNION ALL

select distinct Market, 
       sum(rn) over (partition by market) as [RN], 
       sum(rn) over () as Total,
       cast(CAST(((100.0 * (sum(rn) over (partition by Market))/(sum(rn) over ()))) AS DECIMAL(19,1)) as varchar(10))+ ' ' + '%'  as [Percentage],
       'Budget' as [Type]
from [Budget1]


)xx

PIVOT

(

MIN([Percentage])

FOR [Type] IN ([Actual], [Budget])


) AS pvt_table
group by [Market]

答案 1 :(得分:2)

PIVOT通过将表达式(在您的情况下为Type的一列中的唯一值转换为输出(Actual, Budget)中的多列来旋转表值表达式并运行上 最终输出中所需的任何剩余列值 上需要它们的聚合。

因此,您必须优化内部查询,以便仅选择必要的列:

SELECT [Market], [Actual], [Budget]    
FROM 
(
   SELECT [Market], percentage, type 
   FROM  xxx -- You inner query
) AS src 
PIVOT
(
   MIN([Percentage])
   FOR [Type] IN ([Actual], [Budget])
) AS pvt_table

这意味着您必须省去RNTotal列。