SQL 新列,具有基于列条目的 bool 值

标签 sql oracle

我有下表:

columnA     | columnB     | columnC
-----------------------------------
1           |10           |88
2           |10           |88
3           |10           |88
1           |20           |66
2           |20           |66
3           |20           |66
1           |30           |77
2           |30           |77

我想为 C 列中的每个唯一条目创建一个新列,其 bool 值是否存在,如下所示:

columnA     | columnB   | column88 | column66 | column77 
---------------------------------------------------------
1           |10         |True      |True      |True
2           |20         |True      |True      |True
3           |30         |True      |True      |False

如何实现这样的表格?

最佳答案

您可以使用条件聚合

select colA, colB, 
       max(case when colC=88 then 'True' else 'False' end) as col88,
       max(case when colC=66 then 'True' else 'False' end) as col66,
       max(case when colC=77 then 'True' else 'False' end) as col77
from tablename
group by colA, colB

关于SQL 新列,具有基于列条目的 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64458087/

相关文章:

java - JPA @GenerateValue 与 PostgreSQL 生成的序列值中的差距

sql - 如何为主键列创建自定义自动生成的 ID 号?

oracle - ORA-31600 : invalid input value CHAIN for parameter OBJECT_TYPE

oracle - ORA-01465 : invalid hex number in oracle while using BLOB

oracle - SQL*Plus 输入 : How do you get input from user into varchar2 variable?

mysql - 如何计算平均值?

mysql - 计算通过左外连接连接的两个表

java - 为什么hibernate在insert和delete操作时会生成 'T_' prefix tableName?

C# - 我应该使用静态数据库连接吗

sql - 如何在 SQL Developer 中为多值参数输入绑定(bind)