我有需要转为网格视图的数据。它的 2 行连接成一行,然后旋转成列。
下面是数据设置。我只包括了 1 月到 4 月,但这将持续几个月,而且年份可能会改变,但它总是连续 2 年。
Sample Data
CREATE TABLE #tmpData (
[YEAR] INT,
[AMOUNT] DECIMAL(12,2),
[PAX] INT,
[PRODUCTID] INT,
[MONTH] INT,
[MONTHNAME] VARCHAR(10)
)
INSERT INTO #tmpData SELECT 2012 ,3309 ,10 ,1 ,1 ,'January'
INSERT INTO #tmpData SELECT 2013 ,3257.25 ,11 ,1 ,1 ,'January'
INSERT INTO #tmpData SELECT 2012 ,4351.2 ,21 ,1 ,2 ,'February'
INSERT INTO #tmpData SELECT 2013 ,3719.25 ,31 ,1 ,2 ,'February'
INSERT INTO #tmpData SELECT 2012 ,4687 ,11 ,1 ,3 ,'March'
INSERT INTO #tmpData SELECT 2013 ,4120.74 ,11 ,1 ,3 ,'March'
INSERT INTO #tmpData SELECT 2012 ,6123.1 ,21 ,1 ,4 ,'April'
INSERT INTO #tmpData SELECT 2013 ,5417.25 ,21 ,1 ,4 ,'April'
INSERT INTO #tmpData SELECT 2012 ,5416.5 ,10 ,3 ,1 ,'January'
INSERT INTO #tmpData SELECT 2013 ,6104.6 ,20 ,3 ,1 ,'January'
INSERT INTO #tmpData SELECT 2012 ,9748.16 ,30 ,3 ,2 ,'February'
INSERT INTO #tmpData SELECT 2013 ,10797.43 ,30 ,3 ,2 ,'February'
INSERT INTO #tmpData SELECT 2012 ,12706.32 ,30 ,3 ,3 ,'March'
INSERT INTO #tmpData SELECT 2013 ,13194.3 ,4 ,3 ,3 ,'March'
INSERT INTO #tmpData SELECT 2012 ,16429.03 ,33 ,3 ,4 ,'April'
INSERT INTO #tmpData SELECT 2013 ,14339.92 ,37 ,3 ,4 ,'April'
SELECT * FROM #tmpData
DROP TABLE #tmpData
期望的结果
CREATE TABLE #tmpResults (
[PRODUCTID] INT,
[2013_JAN_AMOUNT] DECIMAL(12,2),
[2013_JAN_AMOUNT_%] INT,
[2013_JAN_PAX] INT,
[2013_JAN_PAX_%] INT,
[2013_FEB_AMOUNT] DECIMAL(12,2),
[2013_FEB_AMOUNT_%] INT,
[2013_FEB_PAX] INT,
[2013_FEB_PAX_%] INT,
[2013_MAR_AMOUNT] DECIMAL(12,2),
[2013_MAR_AMOUNT_%] INT,
[2013_MAR_PAX] INT,
[2013_MAR_PAX_%] INT,
[2013_APR_AMOUNT] DECIMAL(12,2),
[2013_APR_AMOUNT_%] INT,
[2013_APR_PAX] INT,
[2013_APR_PAX_%] INT
)
INSERT INTO #tmpResults
SELECT 1, -- PRODUCTID
--AMOUNT CALC= (JAN2013-JAN2012)/JAN2012 2013PAX CALC= (JAN2013-JAN2012)/JAN2012
3257.25, ROUND((SELECT ((3257.25-3309)/3309)*100),0), 11, CONVERT(INT,ROUND(CONVERT(DECIMAL,11-10)/10*100,0))--JAN2013
,3719.25, ROUND((SELECT ((3719.25-4351.2)/4351.2)*100),0), 31, CONVERT(INT,ROUND(CONVERT(DECIMAL,31-21)/21*100,0)) --FEB2013
,4120.74, ROUND((SELECT ((4120.74-4687)/4687)*100),0), 11, CONVERT(INT,ROUND(CONVERT(DECIMAL,11-10)/10*100,0)) --MAR2013
,5417.25, ROUND((SELECT ((5417.25-6123.1)/6123.1)*100),0), 21, CONVERT(INT,ROUND(CONVERT(DECIMAL,11-10)/10*100,0)) --APR2013
INSERT INTO #tmpResults
SELECT 3, -- PRODUCTID
--AMOUNT CALC= (JAN2013-JAN2012)/JAN2012 2013PAX CALC= (JAN2013-JAN2012)/JAN2012
6104.6, ROUND((SELECT ((6104.6-5416.5)/5416.5)*100),0), 20, ROUND((SELECT ((20-10)/10)*100),2) --JAN2013
,10797.43, ROUND((SELECT ((10797.43-9748.16)/9748.16)*100),0), 30, ROUND((SELECT ((30-30)/30)*100),2) --FEB2013
,13194.3, ROUND((SELECT ((13194.3-12706.32)/12706.32)*100),0), 4, ROUND((SELECT ((4-30)/30)*100),2) --MAR2013
,14339.92, ROUND((SELECT ((14339.92-16429.03)/16429.03)*100),0), 37, ROUND((SELECT ((37-33)/33)*100),2) --APR2013
SELECT * FROM #tmpResults
DROP TABLE #tmpResults
这是@ SQLFiddle 的数据 - http://sqlfiddle.com/#!6/e8ed1/7/1
有什么建议吗?