如何在Teradata中将3条记录压平为1条?

时间:2019-04-03 11:49:13

标签: arrays teradata flatten

我有一个Teradata查询,它将在一个公共字段中最多将3种不同类型的数据输出到3个单独的记录中。我目前正在使用Case语句将这些公共字段分解为唯一字段,但是,输出仍然是每人3条记录。

这是下一部分中描述的当前Case语句的输出:

我有显示当前数据集的图像,但是没有信誉点可以发布它们。

这是当前的Case语句,其结果是根据“测试名称”将每组测试信息放入正确的列中:

,CASE WHEN Test = 'Biology' THEN BiologyName ELSE null END As "Biology Test Name"
,CASE WHEN Test = 'Biology' THEN BiologyDate ELSE null END as "Biology Test Date"
,CASE WHEN Test = 'Biology' THEN BiologyResult ELSE null END as "Biology Test Result"

,CASE WHEN Test = 'Calculus' THEN CalculusName ELSE null END As "Calculus Test Name"
,CASE WHEN Test = 'Calculus' THEN CalculusDate ELSE null END as "Calculus Test Date"
,CASE WHEN Test = 'Calculus' THEN CalculusResult ELSE null END as "Calculus Test Result"

,CASE WHEN Test = 'Language Arts' THEN LanguageArtsName ELSE null END As "LA Test Name"
,CASE WHEN Test = 'Language Arts' THEN LanguageArtsDate ELSE null END as "LA Test Date"
,CASE WHEN Test = 'Language Arts' THEN LanguageArtsResult ELSE null END as "LA Test Result"

考虑下图,其中左侧数据(Cols A-D)是当前输出,而Cols F-O是所需的输出:

我有显示当前数据集的图像,但是没有信誉点可以发布它们。

1 个答案:

答案 0 :(得分:3)

您将要关闭:-)

只需GROUP BY name并应用MAX:

,Max(CASE WHEN Test = 'Biology' THEN BiologyName END) AS "Biology Test Name"
,Max(CASE WHEN Test = 'Biology' THEN BiologyDate END) AS "Biology Test Date"
,Max(CASE WHEN Test = 'Biology' THEN BiologyResult END) AS "Biology Test Result"

,Max(CASE WHEN Test = 'Calculus' THEN CalculusName END) AS "Calculus Test Name"
,Max(CASE WHEN Test = 'Calculus' THEN CalculusDate END) AS "Calculus Test Date"
,Max(CASE WHEN Test = 'Calculus' THEN CalculusResult END) AS "Calculus Test Result"

,Max(CASE WHEN Test = 'Language Arts' THEN LanguageArtsName END) AS "LA Test Name"
,Max(CASE WHEN Test = 'Language Arts' THEN LanguageArtsDate END) AS "LA Test Date"
,Max(CASE WHEN Test = 'Language Arts' THEN LanguageArtsResult END) AS "LA Test Result"

不需要ELSE NULL,这仍然是默认设置。