gpt4 book ai didi

mysql - 由于版本错误,如何在不使用 CTE 的情况下重写 MySQL CTE 查询?

转载 作者:行者123 更新时间:2023-11-29 07:17:02 29 4
gpt4 key购买 nike

我的 CTE 出现错误。我在 Amazon RDS 上托管数据库。我的计算机上有另一个示例数据库主机 (localhost)。 CTE 在我的本地计算机上运行良好。我认为 Amazon RDS 有问题。也许它不支持 CTE。有人遇到同样的问题吗?知道如何解决此问题或如何在没有 CTE 的情况下重写此查询?对此的任何帮助将不胜感激。

    WITH StoreSku AS
(
SELECT
S.StoreName
, RTRIM(LTRIM(LEFT(S.StoreName, 4))) 'StoreNumber'
, P.Sku
, P.Description
FROM simplymac_staging.LocationMasterList S
CROSS JOIN simplymac_staging.`dbo.Sku` P
WHERE S.Disabled = 0
),
Inventory AS
(
SELECT
I.StoreName
, RTRIM(LTRIM(LEFT(I.StoreName, 4))) 'StoreNumber'
, I. ProductName
, I.ProductIdentifier
, I.UnitCost
, SUM(I.Quantity) 'Quantity'
, I.BinStatus
FROM simplymac_staging.inventorylistinstores I
GROUP BY
I.ProductIdentifier
, I.ProductName
, I.BinStatus
, I.StoreName
, I.UnitCost
)
SELECT
P.StoreName
, P.Description
, P.Sku
, CASE WHEN I.BinStatus in ('String_InStock') THEN I.Quantity ELSE 0 END ' In Stock'
, CASE WHEN I.BinStatus in ('String_TransferIn') THEN I.Quantity ELSE 0 END 'TransferIn'
, CASE WHEN I.BinStatus in ('String_TransferOut') THEN I.Quantity ELSE 0 END 'TransferOut'
, CASE WHEN I.BinStatus in ('String_OnOrder') THEN I.Quantity ELSE 0 END 'OnOrder'
FROM StoreSku P
LEFT JOIN Inventory I ON I.ProductIdentifier = P.ID AND I.StoreNumber = P.StoreNumber

错误

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Stores AS ( SELECT S.StoreName , RTRIM(LTRIM(LEFT(S.StoreName, 4))) 'St' at line 1

再次强调,如果能在没有 CTE 的情况下重写我的查询,我们将不胜感激。非常感谢你

最佳答案

作为一个简单的解决方案:由于两个 CTE 显然彼此无关,因此不涉及递归,并且每个 CTE 在主查询中仅从中选择一次,那么您可以将它们转换为内联表,即移动它们到 FROM 子句,像这样:

SELECT 
P.StoreName
, P.Description
, P.Sku
, CASE WHEN I.BinStatus in ('String_InStock') THEN I.Quantity ELSE 0 END ' In Stock'
, CASE WHEN I.BinStatus in ('String_TransferIn') THEN I.Quantity ELSE 0 END 'TransferIn'
, CASE WHEN I.BinStatus in ('String_TransferOut') THEN I.Quantity ELSE 0 END 'TransferOut'
, CASE WHEN I.BinStatus in ('String_OnOrder') THEN I.Quantity ELSE 0 END 'OnOrder'
FROM
(
SELECT
S.StoreName
, RTRIM(LTRIM(LEFT(S.StoreName, 4))) 'StoreNumber'
, P.Sku
, P.Description
FROM simplymac_staging.LocationMasterList S
CROSS JOIN simplymac_staging.`dbo.Sku` P
WHERE S.Disabled = 0
) P
LEFT JOIN (
(
SELECT
I.StoreName
, RTRIM(LTRIM(LEFT(I.StoreName, 4))) 'StoreNumber'
, I. ProductName
, I.ProductIdentifier
, I.UnitCost
, SUM(I.Quantity) 'Quantity'
, I.BinStatus
FROM simplymac_staging.inventorylistinstores I
GROUP BY
I.ProductIdentifier
, I.ProductName
, I.BinStatus
, I.StoreName
, I.UnitCost
) I ON I.ProductIdentifier = P.ID AND I.StoreNumber = P.StoreNumber

关于mysql - 由于版本错误,如何在不使用 CTE 的情况下重写 MySQL CTE 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58698262/

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