我有下表:
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/