2

我正在尝试将以下查询从基于 SQL 服务器的代码转换为 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部分是做什么的。

任何帮助表示赞赏:)

4

2 回答 2

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为每一列获取以下内容的 a 来复制:

  • Hit_Time_GMT对于每个字段
  • Search每列的值(Evar02_SearchPost_Evar02等)
  • just中的最后一个字段UNION ALL指定值来自哪一列
于 2012-08-08T11:48:14.407 回答
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
;
于 2017-04-20T02:25:45.413 回答