[英]How do I use an arithmetic expression in the where clause of a SQL query that has a Group By clause without getting arithmetic overflow?
I have an Azure SQL DB and my query keeps throwing an arithmetic overflow error when it runs over too many rows. 我有一个Azure SQL DB,当它运行太多行时,查询不断抛出算术溢出错误。 This is a sanitized version of my query (SET_END_TIME and SET_START_TIME are FLOAT type):
这是我查询的净化版本(SET_END_TIME和SET_START_TIME是FLOAT类型):
select t1.value1
from TABLE1 t1
join TABLE2 t2
on t1.ID = t2.ID
where
(t1.SET_END_TIME - t1.SET_START_TIME) > 2
and
t2.CREATED_AT > '2017-06-11T00:00:00'
group BY
t1.value1;
If I run this with another and in the where clause that restricts the date to two days, it runs fine. 如果我与另一个运行此操作,并且在where子句中将日期限制为两天,则可以正常运行。 Eg if I added
例如,如果我添加了
and t2.CREATED_AT < '2017-06-13T00:00:00'
So I know the issue is somehow related to the number of rows it is running over, but I don't understand why since the actual arithmetic operation should only be done row by row. 因此,我知道问题在某种程度上与其运行的行数有关,但是我不明白为什么,因为实际的算术运算只能逐行进行。 Does anyone know what could be causing the overflow error and how I can fix my query to be able to run over many rows?
有谁知道是什么可能导致溢出错误,以及如何解决我的查询以使其能够在许多行上运行?
EDIT: I know I have invalid values. 编辑:我知道我有无效的值。 I can see SET_START_TIME values of 1.79769313486232E+308 and -8.98846567431158E+307.
我可以看到SET_START_TIME值为1.79769313486232E + 308和-8.98846567431158E + 307。 Is there anyway I can tell the query to ignore invalid values (eg any Set start time > 100 and < 0) so it won't try to run the arithmetic on them?
无论如何,我可以告诉查询忽略无效值(例如,任何Set起始时间> 100和<0),以便它不会尝试对它们运行算术吗?
I would be suspicious of the values in SET_END_TIME and SET_START_TIME and that calculation causing you the error. 我会怀疑SET_END_TIME和SET_START_TIME中的值以及该计算会导致您出错。 Can you output each column and see if perhaps SET_START_TIME is negative at some point and that produces the error you get?
您可以输出每一列,看看SET_START_TIME有时是否为负值,并且会产生错误?
Try removing the group by. 尝试删除分组依据。
If you want distinct values for t1.value add the distinct keyword in the select. 如果要为t1.value提供不同的值,请在select中添加distinct关键字。
(just thinking out loud) (只是大声思考)
Turns out the root cause was invalid values in the columns I was using for the arithmetic in my where clause, and the solution was to use a case expression inside a subquery to first sanitize the values I want to perform the arithmetic on, then filter on them in the outer query. 原来的根本原因是我在where子句中用于算术的列中的值无效,解决方案是在子查询中使用一个case表达式首先清理要对其执行算术的值,然后进行过滤它们在外部查询中。 Example:
例:
select
t1.value1,
count(*) as total_values
from
(
select
t1.ID as innerID,
(case when t1.SET_START_TIME < 10000 and t1.SET_START_TIME > 0 then t1.SET_START_TIME else 0 end) as set_start,
(case when t1.SET_END_TIME < 10000 and t1.SET_END_TIME > 0 then t1.SET_END_TIME else 0 end) as set_end
from TABLE1 t1
) innerTable
join TABLE2 t2
on innerID = t2.ID
where
(set_end - set_start) > 2
and
t2.CREATED_AT > '2017-06-11T00:00:00'
group by
t1.value1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.