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

Make new query to have same effect as previous one

我有以下 table:

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);

哪个returns:

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

问题和问题: 我如何进行另一个查询 returns 与上面相同的结果,但使用像联合和交叉这样的 SET 操作而不是 group by cube(...)?

您将使用:

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 值的一行,它 select 来自 dual.