Presto:Return 另一个 table 或一个虚拟 value/table 如果原始查询 returns 没有行

Presto: Return another table or a dummy value/table if original query returns no rows

我正在 Presto/Hive 学习 SQL。我想问一下,如果我的原始查询 return 没有行,我是否可以 return 另一个 select table?

类似--

if (select ORIGINAL_QUERY returns 没有行) then (select ALTERNATE_QUERY).

尝试次数:

我知道有一个叫做 IF EXISTS 的东西,但我似乎无法让它在 select 语句执行时起作用? Presto 资源有限,如有任何帮助和指导,我们将不胜感激!

这不是家庭作业。只是想解决一些 PrestoSQL 挑战。

您可以使用 union all 和 CTE:

with q1 as (
      <first query here>
     )
select q1.*
from q1
union all
select q2.*
from q2
where not exists (select 1 from q1);

注意:这假定查询 return 相同的列数和类型。