SQL查询失败,如果3个表中的任何一个有至少一条错误记录(错误表示rate column<=0)
Fail SQL query if there is at least one wrong record in any of 3 tables (wrong means rate column<=0)
我有 3 个表,我需要检查它们的“比率”列中是否有正数。
表有不同的架构,但它们都有一个名为“rate”的列。
我正在使用气流,我创建了一个 DAG,它的任务是检查速率是否不是正数然后失败。
所以简而言之,我希望我的 SQL 失败,如果这些表格中的任何一个比率不正确。
我这样做了:
WITH t1 AS (
SELECT deliverydate, rate
FROM table1
WHERE rate <= 0
),
t2 AS (
SELECT deliverydate, rate
FROM table2
WHERE rate <= 0
),
t3 AS (
SELECT deliverydate, rate
FROM table3
WHERE rate <= 0
)
SELECT 1 FROM (
SELECT * FROM t1
UNION ALL
SELECT * FROM t2
UNION ALL
SELECT * FROM t3
)
在这种情况下,t1、t2 和 t3 将报告错误率(0 或负率)的行。而Select1如果没有错误率则失败
但我的目标是:如果所有比率都正确则通过,如果至少有一个记录有错误比率则失败并告诉我该记录以修复它。
SQL 在 bigquery 标准
上查询 运行
我会建议这样的逻辑:
select (case when exists (select 1 from table1 where rate <= 0) then 'FAIL'
when exists (select 1 from table2 where rate <= 0) then 'FAIL'
when exists (select 1 from table3 where rate <= 0) then 'FAIL'
else 'PASS'
end)
这在第一次失败时停止,应该非常有效。为了提高性能,请在每个子表中包含 rate
的索引。
有些数据库需要 from
子句,例如 from dual
.
我怀疑这是做事的最佳方式,但只有在使用 error()
:
出现错误情况时才应该产生错误
select (case when exists (select 1 from table1 where rate <= 0) then error('Bad rate in table1')
when exists (select 1 from table2 where rate <= 0) then error('Bad rate in table2')
when exists (select 1 from table3 where rate <= 0) then error('Bad rate in table3')
else 'PASS'
end)
我有 3 个表,我需要检查它们的“比率”列中是否有正数。 表有不同的架构,但它们都有一个名为“rate”的列。 我正在使用气流,我创建了一个 DAG,它的任务是检查速率是否不是正数然后失败。 所以简而言之,我希望我的 SQL 失败,如果这些表格中的任何一个比率不正确。 我这样做了:
WITH t1 AS (
SELECT deliverydate, rate
FROM table1
WHERE rate <= 0
),
t2 AS (
SELECT deliverydate, rate
FROM table2
WHERE rate <= 0
),
t3 AS (
SELECT deliverydate, rate
FROM table3
WHERE rate <= 0
)
SELECT 1 FROM (
SELECT * FROM t1
UNION ALL
SELECT * FROM t2
UNION ALL
SELECT * FROM t3
)
在这种情况下,t1、t2 和 t3 将报告错误率(0 或负率)的行。而Select1如果没有错误率则失败
但我的目标是:如果所有比率都正确则通过,如果至少有一个记录有错误比率则失败并告诉我该记录以修复它。
SQL 在 bigquery 标准
上查询 运行我会建议这样的逻辑:
select (case when exists (select 1 from table1 where rate <= 0) then 'FAIL'
when exists (select 1 from table2 where rate <= 0) then 'FAIL'
when exists (select 1 from table3 where rate <= 0) then 'FAIL'
else 'PASS'
end)
这在第一次失败时停止,应该非常有效。为了提高性能,请在每个子表中包含 rate
的索引。
有些数据库需要 from
子句,例如 from dual
.
我怀疑这是做事的最佳方式,但只有在使用 error()
:
select (case when exists (select 1 from table1 where rate <= 0) then error('Bad rate in table1')
when exists (select 1 from table2 where rate <= 0) then error('Bad rate in table2')
when exists (select 1 from table3 where rate <= 0) then error('Bad rate in table3')
else 'PASS'
end)