2014-06-24 206 views
0

我有臺公制數據動態生成列

Metric ID   Metric Date    Metric Result  Metric Calculation Color 
-------------------------------------------------------------------------------------- 
001    2004-04-01     1     GREEN 
002    2004-04-01     0     RED 
001    2004-05-01     1     GREEN 
002    2004-05-01     5     YELLOW 
003    2004-05-01     2     GREEN 

我想結果集類似

Metric ID  April Result   April Calc Color May Result May Calc Color 
--------------------------------------------------------------------------------------- 
001    1      GREEN    1   RED 
002    0      RED     5   YELLOW 
003               2   GREEN 

我已經使用的數據透視表的概念,但它給我的重複的結果就像

Metric ID  April Result   April Calc Color May Result May Calc Color 
--------------------------------------------------------------------------------------- 
001    1      GREEN    NULL  NULL 
001    NULL     NULL     1   RED 
002    0      RED     NULL  NULL 
002    NULL     NULL     5   YELLOW 
003               2   GREEN 

這是我用

查詢
;With CTE AS 
( 
SELECT * FROM (SELECT C.METRIC_ID,RESULT,COLOR FROM METRICDATA) Q PIVOT (MAX(RESULT) FOR [RESULT] IN ([April],[May]))Pv) PIVOT (MAX(COLOR) FOR [RESULT] IN ([April],[May]))Pv1) 

度量結果和計算顏色列應根據查詢提供的日期限制生成。

在此先感謝。

+0

;隨着CTE AS( SELECT * FROM ( SELECT C.METRIC_ID,RESULT,COLOR FROM METRICDATA )問 PIVOT (MAX(RESULT) FOR [結果] IN([年4月] ,[日]) )PV )PIVOT (MAX(彩色) FOR [結果] IN([月],[日]) )在Pv1 ) – Amirtha

+0

如果你能得到這一結果你爲什麼不只是最後一次聚合它?像SELECT Metric_ID,MAX(April_Result)AS April_Result,MAX(April_Calc_Color)AS April_Calc_Color,MAX(May_Result)AS May_Result,MAX(May_Calc_Color)AS May_Calc_Color FROM MyPivot GROUP BY Metric_ID – VBlades

+0

我不知道確切的月份範圍它應該作爲參數傳遞如果我正在使用聚合,它將在結果集中單獨採用最新月份的值,並將NULL作爲所有其他月份的值返回。 – Amirtha

回答

0

試試這個,而不是旋轉

Select metricId, 
Case when datepart(Metric_Date)=4 then Metric_Result else o end 'April Result', 
Case when datepart(Metric_Date)=4 then Metric_Color else NULL end 'April Calc Color', 
Case when datepart(Metric_Date)=5 then Metric_Result else o end 'MayResult', 
Case when datepart(Metric_Date)=5 then Metric_Color else NULL end 'May Calc Color' 
from Metric 
0

這很有趣。 SQLFiddle不喜歡我的代碼(我似乎不太喜歡變量),但這在我的本地盒子上工作。這裏的模式來測試:

CREATE TABLE METRICDATA 
    ([Metric_ID] varchar(3), [Date] date, [Result] int, [Color] varchar(6)) 
; 

INSERT INTO METRICDATA 
    ([Metric_ID], [Date], [Result], [Color]) 
VALUES 
    ('001', '2004-04-01', 1, 'GREEN'), 
    ('002', '2004-04-01', 0, 'RED'), 
    ('001', '2004-05-01', 1, 'GREEN'), 
    ('002', '2004-05-01', 5, 'YELLOW'), 
    ('003', '2004-05-01', 2, 'GREEN') 
; 

而這裏的代碼:

DECLARE @StartDate AS DATE, 
     @EndDate AS DATE, 
     @SQL VARCHAR(MAX); 

SET @StartDate = '20040401'; 
SET @EndDate = '20040501'; 

WITH cteWithMonths 
AS 
(
    SELECT  Metric_ID, 
       DATENAME(MONTH, Date) AS MonthName, 
       DATEPART(M, Date) AS Month, 
       MAX(DATEPART(M, Date)) OVER() AS MaxMonth, 
       Color 
    FROM 
       METRICDATA 
    WHERE 
       Date BETWEEN @StartDate AND @EndDate 
    GROUP BY 
       Metric_ID, 
       DATENAME(MONTH, Date), 
       DATEPART(M, Date), 
       Color 
), 
cteSQLFieldList 
AS 
(
    SELECT  0 AS LineNum, 'SELECT DISTINCT m.Metric_ID,' AS SQL 
    UNION ALL 
    SELECT  DISTINCT 
       MONTH, 
       CASE 
        WHEN Month < MaxMonth THEN MonthName + '.' + MonthName + '_Result, ' + MonthName + '.' + MonthName + '_Calc_Color,' 
        ELSE MonthName + '.' + MonthName + '_Result, ' + MonthName + '.' + MonthName + '_Calc_Color' 
       END 
    FROM  cteWithMonths 
    UNION ALL 
    SELECT  13, 
       'FROM METRICDATA m' 
), 
cteSQLJoinList 
AS 
(
    SELECT  DISTINCT 
       MONTH + 13 AS LineNum, 
       'LEFT JOIN (SELECT Metric_ID, SUM(RESULT) AS ' + MonthName + '_Result, Color AS ' + MonthName + '_Calc_Color FROM METRICDATA WHERE DATENAME(MONTH, Date) = ''' + MonthName + ''' GROUP BY Metric_ID, Color) ' + MonthName +' ON m.Metric_ID = ' + MonthName + '.Metric_ID' AS SQL 
    FROM  cteWithMonths 
), 
cteSQLOrderByList 
AS 
(
    SELECT  26 AS LineNum, 
       'ORDER BY m.Metric_ID' AS SQL 
), 
cteSQL_ALL 
AS 
(
    SELECT  * 
    FROM  cteSQLFieldList 
    UNION 
    SELECT  * 
    FROM  cteSQLJoinList 
    UNION 
    SELECT  * 
    FROM  cteSQLOrderByList 
) 

SELECT  @SQL = COALESCE(@SQL + ' ', '') + SQL 
FROM  cteSQL_ALL 
ORDER BY LineNum; 

EXEC(@SQL); 

我不想跟你PIVOT解決方案發揮,要誠實。動態構建它看起來很複雜。我選擇了聚合每個Metric_ID在dervied表然後加入所有他們在一起。我生成必要的動態SQL然後執行它。