简体   繁体   English

如何在具有Group By子句的SQL查询的where子句中使用算术表达式,而不会出现算术溢出?

[英]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.

相关问题 在以下查询中,如何在where子句中使用“ group”进行比较? - How do I use the `group` in where clause for the comparison in following query? MYSQL:如何在不使用子查询的情况下将算术表达式指定为SELECT子句中字段的值? - MYSQL: How can I assign an arithmetic expression to be the value of a field in the SELECT clause without using subqueries? 如何在SQL Developer的查询的WHERE子句中使用unicode? - How do I use unicode in the WHERE clause of a query in SQL Developer? 如何在参数化 SQL 查询中使用多个 WHERE 子句? - How do I use multiple WHERE clause in parameterized SQL query? 如何在SQLAlchemy中的GROUP BY之后对算术表达式进行ORDER? - How do I ORDER BY an arithmetic expression after a GROUP BY in SQLAlchemy? where子句分组依据中的SQL查询 - SQL query in where clause group by 如何在没有算术计数的SQL子查询中使用GROUP BY - How to use GROUP BY in SQL subquery without arithmetic count SQL查询中的算术溢出错误转换表达式(替换和大小写语句) - Arithmetic overflow error converting expression(Replace and Case Statement) in SQL query 如何在 Where 子句中记录没有敏感数据的 SQL 查询? - How do I log a SQL Query without Sensitive Data in Where Clause? 如何在此 SQL 查询中使用 WHERE 子句? - How can I use WHERE clause in this SQL query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM