如何将列值转换为teradata中的列名称?

问题描述:

我有一个表,如下所示:如何将列值转换为teradata中的列名称?

Name  Subjects 
X   math 
Y   science 
Z   english 

我需要在以下格式的报告:

Name  math  science  english 
    X   Y   N   N 
    Y   N   Y   N 
    Z   N   N   Y 

如何实现这一目标使用一个选择查询?

That'a常见的问题,搜索“PIVOT查询” :-)

假设一个名称可以有你需要使用MAX/GROUP BY多个主题,否则只需删除聚集。

select 
    Name, 
    max(case when Subjects = 'math' then 'Y' else 'N' end) as Math, 
    max(case when Subjects = 'science' then 'Y' else 'N' end) as Science, 
    max(case when Subjects = 'english' then 'Y' else 'N' end) as English 
from tab 
group by Name