sql - Turn Multiple Rows into 1 Column with Multiple Fields in Teradata SQL
问题描述
I have a table that looks like this in Teradata SQL Assistant
:
Software Name Employee
Word Bob
Excel Bob
Word Kim
Excel Kim
PowerPoint Bob
I want to create it so that for each software, there is only 1 row and each employee is stored as a binary 1 or 0 based on whether they have the software or not. The Final Table should look like this:
Software Name Bob Kim
Word 1 1
Excel 1 1
PowerPoint 1 0
I'm using Teradata 15.10
and I haven't been able to figure it out how to do this because Teradata 15.10
does not support the PIVOT
function.
解决方案
只需使用条件聚合:
select software_name,
sum(case when employee = 'Bob' then 1 else 0 end) as Bob,
sum(case when employee = 'Kim' then 1 else 0 end) as Kim
from t
group by software_name;
推荐阅读
- r - 基于列名的条件乘法
- sql - 如何根据 SQL serer 中其他表的值更新列中的值?
- c# - 如何从 NFC 卡获取 cardnummer - 而不是 UID
- c# - 在c#中检查字符串是否为空而不会出错
- nhibernate - 使用 MySQL8Dialect 配置 Nhibernate 5.3.8
- python - 使用 Numpy 或 OpenCV 将图像的剪切复制到新图像
- python - Python 结构包镜像
- c++ - 这个 c++ 语法叫什么
- angular - Angular 库“includePaths”替代方案
- c# - 如何使用一个 Dockerfile 处理 multibleproject?