sql - 用逗号分隔列,忽略空值
问题描述
我有下表:
一种 | 乙 | C | D | 乙 |
---|---|---|---|---|
A1 | 空值 | C1 | 空值 | E1 |
A2 | B2 | C2 | 空值 | 空值 |
空值 | 空值 | C3 | 空值 | E3 |
我想要以下输出(用逗号分隔,如果任何值为空,则不要添加逗号):
F |
---|
A1、C1、E1 |
A2、B2、C2 |
C3、E3 |
解决方案
基本上,你想要concat_ws()
——Oracle 不支持。相反,您可以使用:
select trim(',' from
(case when A is not null then ',' || A end ||
case when B is not null then ',' || B end ||
case when C is not null then ',' || C end ||
case when D is not null then ',' || D end ||
case when E is not null then ',' || E end
)
)
推荐阅读
- android - Mapbox Android SDK 使用的 CarmenFeature 是什么?
- html - CSS clip-path 属性与 SVG 路径有问题
- python - 查询 ManyToManyField 速度的更好方法是什么?整个对象还是只有 id?
- ubuntu - 在 Ubuntu 上使用 isql 连接到 MS SQL Server 数据库
- javascript - Mongoose - 保持模型与更改相关联
- java - IntelliJ 随机重新分配实体的数据源
- r - r 获取向量中每个单词的第一个、前两个、前三个字母的列表
- python - Strange behavior with multiple instances of python script still active after interruption
- c - lwip raw api: tracking down double free in embedded systems and other possible issues
- python - Why does graphical window freeze after about 5 seconds?