gpt4 book ai didi

snowflake-cloud-data-platform - 雪花(左连接)横向 : Unsupported subquery type cannot be evaluated

转载 作者:行者123 更新时间:2023-12-04 13:38:21 33 4
gpt4 key购买 nike

Lateral Join

In a FROM clause, the LATERAL keyword allows an in-line view to reference columns from a table expression that precedes that in-line view.

A lateral join behaves more like a correlated subquery than like most JOINs.



让我们稍微调整一下文档中提供的代码:
CREATE TABLE departments (department_id INTEGER, name VARCHAR);
CREATE TABLE employees (employee_ID INTEGER, last_name VARCHAR,
department_ID INTEGER, project_names ARRAY);

INSERT INTO departments (department_ID, name) VALUES
(1, 'Engineering'),
(2, 'Support'),
(3, 'HR'); -- adding new row

INSERT INTO employees (employee_ID, last_name, department_ID) VALUES
(101, 'Richards', 1),
(102, 'Paulson', 1),
(103, 'Johnson', 2);

询问:
SELECT * 
FROM departments AS d,
LATERAL (SELECT * FROM employees AS e
WHERE e.department_ID = d.department_ID
ORDER BY employee_id DESC LIMIT 1) AS iv2 -- adding ORDER BY ... LIMIT ...
ORDER BY employee_ID;

SQL compilation error: Unsupported subquery type cannot be evaluated



是的,我知道我可以用 ROW_NUMBER() 重写这个查询或其他方式。

1) 为什么使用 TOP/LIMIT在这种特殊情况下是不可能的?

2)是否有语法来实现 LEFT JOIN LATERAL/OUTER APPLY ?

即使 LATERAL 子查询不为它们生成任何行,我也希望能够获取结果集中的所有源行。要获得最终结果:
┌────────────────┬──────────────┬──────────────┬────────────┬────────────────┬───────────────┐
│ department_id │ name │ employee_id │ last_name │ department_id │ project_names │
├────────────────┼──────────────┼──────────────┼────────────┼────────────────┼───────────────┤
│ 1 │ Engineering │ 102 │ Paulson │ 1 │ null │
│ 2 │ Support │ 103 │ Johnson │ 2 │ null │
│ 3 │ HR │ null │ null │ null │ null │
└────────────────┴──────────────┴──────────────┴────────────┴────────────────┴───────────────┘

db<>fiddle demo

最佳答案

所以即使我们之前讨论过你知道你可以重写它,这里是重写

WITH departments AS (
SELECT * FROM VALUES
(1, 'Engineering'),
(2, 'Support'),
(3, 'HR')
v(department_ID, name)
), employees AS (
SELECT * FROM VALUES
(101, 'Richards', 1),
(102, 'Paulson', 1),
(103, 'Johnson', 2)
v(employee_ID, last_name, department_ID)
), dep_emp AS (
SELECT *
FROM employees
QUALIFY ROW_NUMBER() OVER (PARTITION BY department_ID ORDER BY employee_id) = 1
)
SELECT *
FROM departments AS d
LEFT JOIN dep_emp AS e ON d.department_ID = e.department_ID
ORDER BY employee_ID;
随心所欲:
DEPARTMENT_ID    NAME           EMPLOYEE_ID    LAST_NAME    DEPARTMENT_ID
1 Engineering 101 Richards 1
2 Support 103 Johnson 2
3 HR null null null
通过从 LATERAL 移动到具有 QUALIFY 的 CTE 以实现 LIMIT/TOP,然后使用 LEFT JOIN 来获取空匹配,您就有了所需的步骤。
对于为什么是这样的未提出问题。 Snowflake 并不是一个真正的每行数据库,它更像是一个 Map/Reduce/MergeJoin 过程,以及简单的相关子查询,它可以重写为多步骤(也就是 CTE/joins),但它不能重写复杂的东西。他们一直在改进它。但是,如果您了解您的数据和您的模型,那么以批量操作来表达事物并让 MergeJoin 的强大功能为您带来胜利是最有意义的。

Is there a syntax to achieve LEFT JOIN LATERAL/OUTER APPLY?this is done via parameter , OUTER => TRUE in the FLATTEN command

关于snowflake-cloud-data-platform - 雪花(左连接)横向 : Unsupported subquery type cannot be evaluated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60477157/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com