【问题标题】:Presto - Union & LimitPresto - 联合和限制
【发布时间】:2021-03-08 11:59:14
【问题描述】:

目前,我有一个 CTE,它正在构建一个数据列表和一个随机行号

我想要做的是根据一些标准输出几个联合在一起的查询。查询可以与联合一起正常工作,但当我为任何查询添加限制时将无法正常工作。

有没有一种方法可以运行查询并获取不同的子集?

例子:

with selection as (
select account, address, type, random(1000)
from details
)

select 
  account,
  address
from details
where type = 'a'
order by random 
limit 50

union all

select 
  account,
  address
from details
where type = 'b'
order by random 
limit 50

union all

select 
  account,
  address
from details
where type = 'c'
order by random 
limit 50

【问题讨论】:

  • 请注意,selection 未使用。

标签: sql union presto


【解决方案1】:

其实这里根本不需要联合:

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY type ORDER BY random) rn
    FROM details
    WHERE type IN ('a', 'b', 'c')
)

SELECT account, address
FROM cte
WHERE rn <= 50;

如果你真的想采用联合方法,那么下面的语法可能会起作用,每个限制子查询都在一个单独的闭包中:

SELECT account, address
FROM
(SELECT account, address
 FROM details
 WHERE type = 'a'
 ORDER BY random)
UNION ALL
(SELECT account, address
 FROM details
 WHERE type = 'b'
 ORDER BY random)
UNION ALL
(SELECT account, address
 FROM details
 WHERE type = 'c'
 ORDER BY random)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 2019-12-21
    • 1970-01-01
    • 2020-09-11
    • 2021-06-20
    • 2017-02-15
    • 1970-01-01
    相关资源
    最近更新 更多