gpt4 book ai didi

azure - Azure Databricks SparkSQL 是否支持递归查询

转载 作者:行者123 更新时间:2023-12-03 02:47:11 26 4
gpt4 key购买 nike

我正在将数据从 SQL Server 移动到 Azure 数据湖第 2 代,并使用递归查询转换 SQL 查询。

这是一个使用 CTE(公用表表达式)进行递归的 SQL 查询示例

 WITH RECURSIVE AS BOM
(SELECT p.MItemId AS RootPartNumber,
p.MItemId AS PartNumber,
NULL AS ParentPartNumber,
0 AS BomLevel,
1.0 AS Quantity
FROM PartItem p

UNION ALL
SELECT BOM.RootPartNumber,
CAST(BSM.ChildItem AS string) AS PartNumber,
CAST(DB.PartNumber AS string) AS ParentPartNumber,
BOM.BomLevel + 1 as BomLevel,
BSM.Quantity AS Quantity
FROM PartItemBomList BSM
INNER JOIN BOM ON BOM.PartNumber = BSM.ParentItem
INNER JOIN PartItem p ON p.MItemId = BSM.ChildItem
WHERE BSM.IsDeleted = 0
)
SELECT * FROM BOM

我尝试更改在 FROM 子句中嵌入递归的查询,如下所示,但没有成功。

 SELECT * FROM 
(SELECT p.MItemId AS RootPartNumber,
p.MItemId AS PartNumber,
NULL AS ParentPartNumber,
0 AS BomLevel,
1.0 AS Quantity
FROM PartItem p
WHERE p.PartType = 'Cloud-OrderableAssembly'
UNION ALL
SELECT BOM.RootPartNumber,
CAST(BSM.ChildItem AS string) AS PartNumber,
CAST(DB.PartNumber AS string) AS ParentPartNumber,
BOM.BomLevel + 1 as BomLevel,
BSM.Quantity AS Quantity
FROM PartItemBomList BSM
INNER JOIN BOM ON BOM.PartNumber = BSM.ParentItem
INNER JOIN PartItem p ON p.MItemId = BSM.ChildItem
WHERE BSM.IsDeleted = 0
) as BOM

这是我从 Azure Databricks session 中收到的错误。

SQL 语句错误:AnalysisException:未找到表或 View :BOM;第 16 行第 22 行

最佳答案

问题就在这里

INNER JOIN BOM  ON BOM.PartNumber = BSM.ParentItem

这是内部查询,据我了解,BOM 是在外部定义的,因此这部分查询运行时 BOM 不存在。

如果我是你,我可能会尝试通过直接在 SQL 上运行来修复以下查询。下面引用BOM的方式不正确

SELECT p.MItemId AS RootPartNumber,
p.MItemId AS PartNumber,
NULL AS ParentPartNumber,
0 AS BomLevel,
1.0 AS Quantity
FROM PartItem p
WHERE p.PartType = 'Cloud-OrderableAssembly'
UNION ALL
SELECT BOM.RootPartNumber,
CAST(BSM.ChildItem AS string) AS PartNumber,
CAST(DB.PartNumber AS string) AS ParentPartNumber,
BOM.BomLevel + 1 as BomLevel,
BSM.Quantity AS Quantity
FROM PartItemBomList BSM
INNER JOIN BOM ON BOM.PartNumber = BSM.ParentItem
INNER JOIN PartItem p ON p.MItemId = BSM.ChildItem
WHERE BSM.IsDeleted = 0

关于azure - Azure Databricks SparkSQL 是否支持递归查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58277764/

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