2013-06-04 51 views
1

我不確定如何將數據轉接到特定視圖。以下是測試數據。SQL - 不支持Aggregates的多個列

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 

正如你可以從輸出的ProductTitle看看是不是分組。

我該如何實現這一目標,還是完全按錯方向?

+0

Microsoft SQL Server 2008(SP2) - 10.0.4000.0(X64) – stevenmahony

回答

2

問題的一部分是,您已經對要轉換的多個列進行了非標準化數據。理想情況下,您應該考慮修復表結構,以便維護和查詢更容易。如果您無法修復表格結構,那麼您應該先取消轉印列,然後應用PIVOT以獲得最終結果。

UNPIVOT進程將採用多個列並將它們轉換爲多行。根據您的SQL Server版本,您可以通過幾種方法來執行此操作。您可以使用UNPIVOT函數,或者因爲您使用的是SQL Server 2008,所以可以使用帶有VALUES子句的CROSS APPLY來反轉。

CROSS應用/值的代碼將是:

select t.producttitle, c.col, c.value 
from tmpData t 
cross apply 
(
    values (abvrMonthName, MonthAvg), (abvrMonthNameCount, MonthCount) 
) c (col, value) 

SQL Fiddle with Demo。這需要你多列和數據放入類似這樣的格式:

| PRODUCTTITLE | COL | VALUE | 
------------------------------- 
| Product 1 | Dec |  0 | 
| Product 1 | Dec# |  0 | 
| Product 1 | Nov |  0 | 
| Product 1 | Nov# |  0 | 
| Product 1 | Oct |  0 | 
| Product 1 | Oct# |  0 | 
| Product 1 | Sep |  0 | 
| Product 1 | Sep# |  0 | 

一旦數據是這種格式,你可以將PIVOT的值col其中包含月份名稱:

select producttitle, jan, [jan#], feb, [feb#], mar, [mar#], apr, [apr#], 
    may, [may#], jun, [jun#], jul, [jul#], aug, [aug#], 
    sep, [sep#], oct, [oct#], nov, [nov#], dec, [dec#] 
from 
(
    select t.producttitle, c.col, c.value 
    from tmpData t 
    cross apply 
    (
    values (abvrMonthName, MonthAvg), (abvrMonthNameCount, MonthCount) 
) c (col, value) 
) d 
pivot 
(
    sum(value) 
    for col in (jan, [jan#], feb, [feb#], mar, [mar#], apr, [apr#], 
       may, [may#], jun, [jun#], jul, [jul#], aug, [aug#], 
       sep, [sep#], oct, [oct#], nov, [nov#], dec, [dec#]) 
) piv; 

參見SQL Fiddle with Demo。這給出了一個結果:

| PRODUCTTITLE | JAN | JAN# | FEB | FEB# | MAR | MAR# | APR | APR# | MAY | MAY# | JUN | JUN# | JUL | JUL# | AUG | AUG# | SEP | SEP# | OCT | OCT# | NOV | NOV# | DEC | DEC# | 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
| Product 1 | 5 | 2 | 5 | 1 | 5 | 4 | 5 | 6 | 4.44 | 9 | 5 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 
| Product 2 | 0 | 0 | 0 | 0 | 0 | 0 | 4.33 | 3 | 4.67 | 3 | 5 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 
| Product 3 | 4.6 | 5 | 4.75 | 8 | 4.75 | 8 | 4 | 6 | 5 | 6 | 5 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 
+0

好吧,虐待給那個前。感謝您的快速回復。 – stevenmahony

+0

完美,完全符合我的需求。非常感謝。 – stevenmahony

+0

另一方面,我修復了表結構,所以不需要交叉應用,但很好知道表結構何時不能修改。再次感謝。 – stevenmahony