。我试图找出如何基于由另一列”的总和除以一个特定列的SUM转动我的数据这里是我到目前为止已经达到,但PIVOT查询给我的错误:
USE MyDatabase
SELECT [PropertyCode],
[Market],
[JANUARY 2015],
[FEBRUARY 2015],
[MARCH 2015]
FROM (SELECT [PropertyCode],
[Market],
[MTH],
[GN],
[PkgRev]
FROM HOLDINGS
) a
PIVOT (Sum([PkgRev])/Sum([GN])
FOR [MTH] IN ([JANUARY 2015],
[FEBRUARY 2015],
[MARCH 2015])) as pvttable
ORDER BY [PROPERTYCODE]
在上面的查询HOLDINGS是VIEW表和PkgRev和GN是在该视图表列。 我需要我的透视查询执行由GN列的总和除以PkgRev列的SUM后返回值。
下面是数据的样本(来自HOLDINGS视图表):
PropertyCode ID GN MTH Pkg Rev Market
A 1 10 Jan-15 95,652 United Kingdom
A 3 20 Jan-15 95,652 United Kingdom
A 12 24 Feb-15 81,610 France
A 15 14 Feb-15 47,606 France
B 16 18 Jan-15 38,153 France
B 20 28 Feb-15 69,391 United Kingdom
B 22 32 Jan-15 107,990 Germany
B 25 28 Feb-15 76,696 United Kingdom
C 26 4 Jan-15 18,961 Germany
C 27 26 Jan-15 56,522 South Africa
C 29 20 Feb-15 27,826 India
C 31 18 Jan-15 33,661 France
C 32 14 Jan-15 26,039 Germany
预期输出如下:
PropertyCode Market Jan-15 Feb-15
A France 0 3,400
A United Kingdom 6,377 0
B France 2,120 0
B Germany 3,375 0
B United Kingdom 0 2,609
C France 1,870 0
C Germany 1,607 0
C India 0 1,391
C South Africa 2,174 0