2012-08-08 54 views
2

我試圖將以下查詢從基於SQL Server的代碼轉換爲TERADATA代碼。Teradata等效於UNPIVOT並從SQL SERVER搜索

INSERT INTO #Keyword(Keyword,OmnitureHitsID,Hit_Time_GMT)  
      SELECT Search, @PrvRowIdentity,Hit_Time_GMT  
      FROM  
      (SELECT  
       Evar02_Search  
      , Hit_Time_GMT  
      , Evar11_End_Keyword  
      , Evar14_End_SrchTrmPassed  
      , Post_Evar02  
      , Post_Evar11  
      , Post_Evar14  
      FROM #MaINTable WHERE ID = @i  
      AND Visid_High [email protected]_High) p  
      UNPIVOT  
      (SEARCH FOR SearchKeyword IN  
       (Evar02_Search  
       , Evar11_End_Keyword  
       , Evar14_End_SrchTrmPassed  
       , Post_Evar02  
       , Post_Evar11  
       , Post_Evar14)  
      ) AS unpvt; 

誰能告訴我如何在UNPIVOT後轉換零件。其實我不確定SEARCH FOR SearchKeyword IN部件是做什麼的。

任何幫助表示讚賞:)

回答

3

不幸的是,Teradata的不具有UNPIVOT功能,但你可以使用UNION ALL查詢複製它:

INSERT INTO #Keyword(Keyword,OmnitureHitsID,Hit_Time_GMT)  
SELECT Search, @PrvRowIdentity, Hit_Time_GMT  
FROM  
(
    SELECT Hit_Time_GMT, Evar02_Search as Search, 'Evar02_Search' as col 
    FROM #MaINTable 
    WHERE ID = @i  
    AND Visid_High [email protected]_High 
    UNION ALL 
    SELECT Hit_Time_GMT, Evar11_End_Keyword as Search, 'Evar11_End_Keyword' as col  
    FROM #MaINTable 
    WHERE ID = @i  
    AND Visid_High [email protected]_High 
    UNION ALL 
    SELECT Hit_Time_GMT, Evar14_End_SrchTrmPassed as Search, 'Evar14_End_SrchTrmPassed' as col  
    FROM #MaINTable 
    WHERE ID = @i  
    AND Visid_High [email protected]_High 
    UNION ALL 
    SELECT Hit_Time_GMT, Post_Evar02 as Search, 'Post_Evar02' as col  
    FROM #MaINTable 
    WHERE ID = @i  
    AND Visid_High [email protected]_High 
    UNION ALL 
    SELECT Hit_Time_GMT, Post_Evar11 as Search, 'Post_Evar11' as col  
    FROM #MaINTable 
    WHERE ID = @i  
    AND Visid_High [email protected]_High 
    UNION ALL 
    SELECT Hit_Time_GMT, Post_Evar14 as Search, 'Post_Evar14' as col  
    FROM #MaINTable 
    WHERE ID = @i  
    AND Visid_High [email protected]_High 
) x 

您當前UNPIVOT查詢的Search的部分是讓列出了每個SearchKeyword列中的列的值。

  • Hit_Time_GMT每個字段爲每一列
  • Search值(Evar02_SearchPost_Evar02等)
  • 最後一個字段:所以這可以通過使用UNION ALL即獲得每列的以下複製在UNION ALL只是指定值從
  • 來到這列
0

的Teradata 14.10+允許這樣的:

select name, subject, marks 
FROM td_unpivot 
(
    ON Student 
    USING 
    VALUE_COLUMNS('Marks') 
    UNPIVOT_COLUMN('Subject') 
    COLUMN_LIST('Maths', 'Science', 'English') 
) u 
;