2

此查询返回包含操作系统信息的两个单独的列,因为数据是基于扫描工具自动写入数据库表的,我无法手动更改列。我正在尝试编写一个将两行合并为一的查询(使用 operating_system_version 行中的数据填充操作系统行中的空白。

我现在的查询如下:

SELECT device_type, operating_system, operating_system_version
FROM DEVICES
WHERE user_name like '|%front-%|'
OR user_name like '|%back-%|'
OR user_name like '|%ap-%|'
OR user_name like '|%me-%|'
OR user_name like '|%mg-%|'

它返回:

device_type    operating_system        operating_system_version


desktop         windows xp
desktop                                 windows 7 professional
desktop         windows xp              
desktop                                 windows 7 professional

我希望它返回如下内容:

device_type    OS


desktop         windows xp
desktop         windows 7 professional                        
desktop         windows xp              
desktop         windows 7 professional  

我试过添加|| 如下所示,但它没有返回数据:

SELECT device_type, operating_system||operating_system_version
FROM DEVICES
WHERE user_name like '|%front-%|'
OR user_name like '|%back-%|'
OR user_name like '|%ap-%|'
OR user_name like '|%me-%|'
OR user_name like '|%mg-%|'                       
4

1 回答 1

4
SELECT device_type, coalesce(operating_system,operating_system_version) AS OS
FROM DEVICES
WHERE user_name like '|%front-%|'
OR user_name like '|%back-%|'
OR user_name like '|%ap-%|'
OR user_name like '|%me-%|'
OR user_name like '|%mg-%|'

这将选择操作系统,或者如果为 null,则选择 operating_system_version 并将其作为列名 OS 返回。

如果要连接这两个字段,请尝试

SELECT device_type, coalesce(operating_system,'') || coalese(operating_system_version,'') AS OS
FROM DEVICES
WHERE user_name like '|%front-%|'
OR user_name like '|%back-%|'
OR user_name like '|%ap-%|'
OR user_name like '|%me-%|'
OR user_name like '|%mg-%|'
于 2013-01-14T18:25:57.207 回答