I am unsure how to pivot my data to the a specific view. Below is the test data.
SQL
CREATE TABLE #tmpData (ProductTitle VARCHAR(100), abvrMonthName VARCHAR(3),abvrMonthNameCount VARCHAR(4),MonthAvg NUMERIC(6,2),MonthCount INT) INSERT INTO #tmpData SELECT 'Product 1','Dec','Dec#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 1','Nov','Nov#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 1','Oct','Oct#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 1','Sep','Sep#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 1','Aug','Aug#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 1','Jul','Jul#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 1','Jun','Jun#', 5 , 1 INSERT INTO #tmpData SELECT 'Product 1','May','May#', 4.44 , 9 INSERT INTO #tmpData SELECT 'Product 1','Apr','Apr#', 5 , 6 INSERT INTO #tmpData SELECT 'Product 1','Mar','Mar#', 5 , 4 INSERT INTO #tmpData SELECT 'Product 1','Feb','Feb#', 5 , 1 INSERT INTO #tmpData SELECT 'Product 1','Jan','Jan#', 5 , 2 INSERT INTO #tmpData SELECT 'Product 2','Dec','Dec#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 2','Nov','Nov#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 2','Oct','Oct#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 2','Sep','Sep#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 2','Aug','Aug#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 2','Jul','Jul#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 2','Jun','Jun#', 5 , 1 INSERT INTO #tmpData SELECT 'Product 2','May','May#', 4.67 , 3 INSERT INTO #tmpData SELECT 'Product 2','Apr','Apr#', 4.33 , 3 INSERT INTO #tmpData SELECT 'Product 2','Mar','Mar#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 2','Feb','Feb#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 2','Jan','Jan#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 3','Dec','Dec#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 3','Nov','Nov#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 3','Oct','Oct#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 3','Sep','Sep#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 3','Aug','Aug#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 3','Jul','Jul#', 0 , 0 INSERT INTO #tmpData SELECT 'Product 3','Jun','Jun#', 5 , 3 INSERT INTO #tmpData SELECT 'Product 3','May','May#', 5 , 6 INSERT INTO #tmpData SELECT 'Product 3','Apr','Apr#', 4 , 6 INSERT INTO #tmpData SELECT 'Product 3','Mar','Mar#', 4.75 , 8 INSERT INTO #tmpData SELECT 'Product 3','Feb','Feb#', 4.75 , 8 INSERT INTO #tmpData SELECT 'Product 3','Jan','Jan#', 4.6 , 5 SELECT ProductTitle,[jan],[jan#],[feb],[feb#] FROM ( SELECT * FROM #tmpData ) AS s PIVOT ( SUM(MonthAvg) FOR abvrMonthName IN ( jan,feb,mar,apr,may,jun,jul,aug, sep, oct, nov, [dec] ) ) as p PIVOT ( SUM(MonthCount) FOR abvrMonthNameCount IN ( jan#,feb#,mar#,apr#,may#,jun#,jul#,aug#, sep#, oct#, nov#, [dec#] ) ) as p1 --GROUP BY ProductTitle,[jan],[feb] DROP TABLE #tmpData
As you can see from the output the ProductTitle
is not grouped.
How would I achieve this or am I going in the wrong direction completely?