Sql 使新查询与上一个查询具有相同的效果
我有下表:Sql 使新查询与上一个查询具有相同的效果,sql,oracle,set-operations,Sql,Oracle,Set Operations,我有下表: Region_id | Region_name 1 Europe 2 Americas 3 Asia 4 Middle East and Africa 此查询: SELECT region_id, region_name FROM hr.regions GROUP BY CUBE(region_id, region_name); 返回: Region_id | Region_name (null
Region_id | Region_name
1 Europe
2 Americas
3 Asia
4 Middle East and Africa
此查询:
SELECT region_id, region_name
FROM hr.regions
GROUP BY CUBE(region_id, region_name);
返回:
Region_id | Region_name
(null) (null)
(null) Asia
(null) Europe
(null) Americas
(null) Middle East and Africa
1 (null)
1 Europe
2 (null)
2 Americas
3 (null)
3 Asia
4 (null)
4 Middle East and Africa
问题和问题:如何进行另一个查询,返回与上述相同的结果,但使用union和intersect等集合操作,而不是
按多维数据集分组(…)
?您将使用:
select region_id, region_name
from hr_regions
union all
select NULL as region_id, region_name
from hr_regions
union all
select region_id, NULL as region_name
from hr_regions
union all
select NULL as region_id, NULL as region_name
from dual;
您的示例数据在任一列中都没有重复项。因此不需要聚合或select distinct
。唯一的例外是最后一个查询。要仅获取一行包含两个NULL
值,请从dual
中进行选择,您可以使用:
select region_id, region_name
from hr_regions
union all
select NULL as region_id, region_name
from hr_regions
union all
select region_id, NULL as region_name
from hr_regions
union all
select NULL as region_id, NULL as region_name
from dual;
您的示例数据在任一列中都没有重复项。因此不需要聚合或select distinct
。唯一的例外是最后一个查询。要仅获取包含两个NULL
值的一行,请从dual
中进行选择