我正在尝试将以下查询从基于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
部分是做什么的。
感谢任何帮助:)
答案 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
Search
值(Evar02_Search
,Post_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
;