SQL 查询在 Azure 中不起作用 SQL 数据仓库 (Synapse) 为什么不呢?
SQL Query doesn't work in Azure SQL Data Warehouse (Synapse) why not?
我有一个在本地 SQL 数据库中运行的 SQL 查询,但是当我尝试在 Azure SQL 数据仓库上执行它时,出现错误.
有谁知道另一种编写此 SQL 查询的方法,以便它可以在 Azure DW 中运行?
SQL查询是:
SELECT MAX(Dates) MostRecentDate FROM (VALUES('2020-01-01'), ('2021-01-01')) AS t(Dates)
Azure DW 错误是:
消息 103010,级别 16,状态 1,第 1 行
第 1 行的解析错误,第 39 列:'('.
附近的语法不正确
这是完整的 SQL 查询(真实 table 姓名已被删除)
SELECT t1.reference, dd.closest_date, d1.date_one, d2.date_two, d3.date_three
FROM dbo.table1 AS t1
LEFT JOIN dbo.table2 AS t2 ON t1.table2_id = t2.id
LEFT JOIN dbo.table3 AS t3 ON t1.table3_id = t3.id
LEFT JOIN dbo.table4 AS t4 ON t3.table4_id = t4.id
LEFT JOIN dbo.table5 AS t5 ON t4.table5_id = t5.id
LEFT JOIN dbo.table6 AS t6 ON t5.table6_id = t6.id
OUTER APPLY (SELECT CASE WHEN t3.outcome IS NULL THEN '5000-01-01' ELSE ISNULL(t3.outcome_date,'5000-01-01') END AS date_one) AS d1
OUTER APPLY (SELECT ISNULL(t2.outcome_date,'5000-01-01') AS date_two) AS d2
OUTER APPLY (SELECT ISNULL(t6.outcome_date,'5000-01-01') AS date_three) AS d3
/*the below works in normal SQL, but doesn't work in Azure SQL!!!*/
-- OUTER APPLY (SELECT MIN(Dates) closest_date FROM (VALUES(d1.date_one),(d2.date_two),(d3.date_three)) AS t(Dates)) AS dd
你能试试这个吗
select max(t.dates)from
(
select '2020-01-01'as dates
union all
select '2021-01-01'
)as t
Table Value Constructor is nice but is not fully supported in Azure Synapse Analytics as per the documentation, ie Synapse is absent in the Applies to list at the top of the linked doc. VALUES
is supported for single rows as per this example,但对您的示例最简单的修复是简单地重写为简单的 UNION ALL
语句,例如
SELECT t1.reference, dd.closest_date, d1.date_one, d2.date_two, d3.date_three
FROM dbo.table1 AS t1
LEFT JOIN dbo.table2 AS t2 ON t1.table2_id = t2.id
LEFT JOIN dbo.table3 AS t3 ON t1.table3_id = t3.id
LEFT JOIN dbo.table4 AS t4 ON t3.table4_id = t4.id
LEFT JOIN dbo.table5 AS t5 ON t4.table5_id = t5.id
LEFT JOIN dbo.table6 AS t6 ON t5.table6_id = t6.id
OUTER APPLY (SELECT CASE WHEN t3.outcome IS NULL THEN '5000-01-01' ELSE ISNULL(t3.outcome_date,'5000-01-01') END AS date_one) AS d1
OUTER APPLY (SELECT ISNULL(t2.outcome_date,'5000-01-01') AS date_two) AS d2
OUTER APPLY (SELECT ISNULL(t6.outcome_date,'5000-01-01') AS date_three) AS d3
/*the below works in normal SQL, but doesn't work in Azure Synapse*/
--OUTER APPLY (SELECT MIN(Dates) closest_date FROM (VALUES(d1.date_one),(d2.date_two),(d3.date_three)) AS t(Dates)) AS dd
OUTER APPLY (SELECT MIN(Dates) closest_date FROM ( SELECT d1.date_one UNION ALL SELECT d2.date_two UNION ALL SELECT d3.date_three) AS t(Dates)) AS dd
这真的只是语法糖。都是一天结束时的集合操作。
我有一个在本地 SQL 数据库中运行的 SQL 查询,但是当我尝试在 Azure SQL 数据仓库上执行它时,出现错误. 有谁知道另一种编写此 SQL 查询的方法,以便它可以在 Azure DW 中运行?
SQL查询是:
SELECT MAX(Dates) MostRecentDate FROM (VALUES('2020-01-01'), ('2021-01-01')) AS t(Dates)
Azure DW 错误是:
消息 103010,级别 16,状态 1,第 1 行 第 1 行的解析错误,第 39 列:'('.
附近的语法不正确这是完整的 SQL 查询(真实 table 姓名已被删除)
SELECT t1.reference, dd.closest_date, d1.date_one, d2.date_two, d3.date_three
FROM dbo.table1 AS t1
LEFT JOIN dbo.table2 AS t2 ON t1.table2_id = t2.id
LEFT JOIN dbo.table3 AS t3 ON t1.table3_id = t3.id
LEFT JOIN dbo.table4 AS t4 ON t3.table4_id = t4.id
LEFT JOIN dbo.table5 AS t5 ON t4.table5_id = t5.id
LEFT JOIN dbo.table6 AS t6 ON t5.table6_id = t6.id
OUTER APPLY (SELECT CASE WHEN t3.outcome IS NULL THEN '5000-01-01' ELSE ISNULL(t3.outcome_date,'5000-01-01') END AS date_one) AS d1
OUTER APPLY (SELECT ISNULL(t2.outcome_date,'5000-01-01') AS date_two) AS d2
OUTER APPLY (SELECT ISNULL(t6.outcome_date,'5000-01-01') AS date_three) AS d3
/*the below works in normal SQL, but doesn't work in Azure SQL!!!*/
-- OUTER APPLY (SELECT MIN(Dates) closest_date FROM (VALUES(d1.date_one),(d2.date_two),(d3.date_three)) AS t(Dates)) AS dd
你能试试这个吗
select max(t.dates)from
(
select '2020-01-01'as dates
union all
select '2021-01-01'
)as t
Table Value Constructor is nice but is not fully supported in Azure Synapse Analytics as per the documentation, ie Synapse is absent in the Applies to list at the top of the linked doc. VALUES
is supported for single rows as per this example,但对您的示例最简单的修复是简单地重写为简单的 UNION ALL
语句,例如
SELECT t1.reference, dd.closest_date, d1.date_one, d2.date_two, d3.date_three
FROM dbo.table1 AS t1
LEFT JOIN dbo.table2 AS t2 ON t1.table2_id = t2.id
LEFT JOIN dbo.table3 AS t3 ON t1.table3_id = t3.id
LEFT JOIN dbo.table4 AS t4 ON t3.table4_id = t4.id
LEFT JOIN dbo.table5 AS t5 ON t4.table5_id = t5.id
LEFT JOIN dbo.table6 AS t6 ON t5.table6_id = t6.id
OUTER APPLY (SELECT CASE WHEN t3.outcome IS NULL THEN '5000-01-01' ELSE ISNULL(t3.outcome_date,'5000-01-01') END AS date_one) AS d1
OUTER APPLY (SELECT ISNULL(t2.outcome_date,'5000-01-01') AS date_two) AS d2
OUTER APPLY (SELECT ISNULL(t6.outcome_date,'5000-01-01') AS date_three) AS d3
/*the below works in normal SQL, but doesn't work in Azure Synapse*/
--OUTER APPLY (SELECT MIN(Dates) closest_date FROM (VALUES(d1.date_one),(d2.date_two),(d3.date_three)) AS t(Dates)) AS dd
OUTER APPLY (SELECT MIN(Dates) closest_date FROM ( SELECT d1.date_one UNION ALL SELECT d2.date_two UNION ALL SELECT d3.date_three) AS t(Dates)) AS dd
这真的只是语法糖。都是一天结束时的集合操作。