Teradata与SQL SERVER的UNPIVOT和SEARCH FOR相当

时间:2012-08-08 06:28:26

标签: sql sql-server database teradata unpivot

我正在尝试将以下查询从基于SQL Server的代码转换为TERADATA代码。

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 =@Visid_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部分是做什么的。

感谢任何帮助:)

2 个答案:

答案 0 :(得分: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 =@Visid_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 =@Visid_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 =@Visid_High 
  UNION ALL
  SELECT Hit_Time_GMT, Post_Evar02 as Search, 'Post_Evar02' as col     
  FROM #MaINTable 
  WHERE ID = @i    
    AND Visid_High =@Visid_High 
  UNION ALL
  SELECT Hit_Time_GMT, Post_Evar11 as Search, 'Post_Evar11' as col     
  FROM #MaINTable 
  WHERE ID = @i    
    AND Visid_High =@Visid_High 
  UNION ALL
  SELECT Hit_Time_GMT, Post_Evar14 as Search, 'Post_Evar14' as col     
  FROM #MaINTable 
  WHERE ID = @i    
    AND Visid_High =@Visid_High 
) x

当前Search查询的UNPIVOT部分是获取列出的每个SearchKeyword列中列的值。因此,可以使用UNION ALL复制每个列的以下内容:

    每个字段
  • Hit_Time_GMT
  • 每个列的{li> Search值(Evar02_SearchPost_Evar02等)
  • UNION ALL中的最后一个字段只指定值来自哪个列

答案 1 :(得分: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
;