pau*_*uld 7 sql amazon-web-services presto amazon-athena
我正在运行一个查询,该查询提供了一组非重叠的first_party_id - 与一个第三方相关联但不与另一个相关联的ID.但是,此查询不会在Athena中运行,从而产生错误:Correlated queries not yet supported.
正在查看prestodb文档, https://prestodb.io/docs/current/sql/select.html(Athena是引擎盖下的prestodb),用于替代嵌套查询.with statement给出的 例子对于这个not in条款似乎没有很好的解释.想知道嵌套查询的替代方法是什么 - 查询如下.
SELECT
COUNT(DISTINCT i.third_party_id) AS uniques
FROM
db.ids i
WHERE
i.third_party_type = 'cookie_1'
AND i.first_party_id NOT IN (
SELECT
i.first_party_id
WHERE
i.third_party_id = 'cookie_2'
)
Run Code Online (Sandbox Code Playgroud)
可能有更好的方法来做到这一点 - 我很想看到它!我能想到的一种方法是使用外连接.(我不确定你的数据是如何构建的,所以请原谅这个人为的例子,但我希望它可以翻译好.)这个怎么样?
with
a as (select *
from (values
(1,'cookie_n',10,'cookie_2'),
(2,'cookie_n',11,'cookie_1'),
(3,'cookie_m',12,'cookie_1'),
(4,'cookie_m',12,'cookie_1'),
(5,'cookie_q',13,'cookie_1'),
(6,'cookie_n',13,'cookie_1'),
(7,'cookie_m',14,'cookie_3')
) as db_ids(first_party_id, first_party_type, third_party_id, third_party_type)
),
b as (select first_party_type
from a where third_party_type = 'cookie_2'),
c as (select a.third_party_id, b.first_party_type as exclude_first_party_type
from a left join b on a.first_party_type = b.first_party_type
where a.third_party_type = 'cookie_1')
select count(distinct third_party_id) from c
where exclude_first_party_type is null;
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!