使新查询具有与上一个相同的效果

问题描述 投票:0回答:1

我有下表:

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

问题与问题:如何进行另一个查询,返回与上述相同的结果,但使用诸如联合和相交之类的SET操作而不是group by cube(...)

sql oracle set-operations
1个回答
2
投票

您将使用:

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中进行选择。

© www.soinside.com 2019 - 2024. All rights reserved.