(错误 1366 SQL)不正确的 DECIMAL 值“()”

(Error 1366 SQL) Incorrect DECIMAL value '()'

我正在为 WindRose 开发 SP,当我尝试 运行 SP 时,SQL 向我显示此错误。

(Error 1366 SQL) Incorrect DECIMAL value '()' for column "" at row -1

这个问题是在修改了我收集数据的table之后出现的。在我为另一个 table 制作此 SP 之前它工作正常,但我必须更改 table 因为这是接收实时数据的那个。

CREATE TEMPORARY TABLE viento_count(angulo DECIMAL, val1 INT, val2 INT, val3 INT, val4 INT, val5 INT, val6 INT);
    
    myloop: LOOP
    
    IF angle > 350 THEN
        LEAVE myloop;
    END IF;

    SET angle1 = angle;
    SET angle2 = angle + 15;
    SET angle_grf = angle + 15/2;
    
    
    SET valor1 = (SELECT COUNT(*) FROM vw_wind_counter WHERE speedy <= 3 AND (device_id = codigo_estacion) AND (data_time BETWEEN fecha_consulta_first_day AND fecha_consulta_last_day) AND direccion > angle1 AND direccion <= angle2);
    SET valor2 = (SELECT COUNT(*) FROM vw_wind_counter WHERE speedy > 3 AND speedy <= 6 AND (device_id = codigo_estacion) AND (data_time BETWEEN fecha_consulta_first_day AND fecha_consulta_last_day) AND direccion > angle1 AND direccion <= angle2);
    SET valor3 = (SELECT COUNT(*) FROM vw_wind_counter WHERE speedy > 6 AND speedy <= 9 AND (device_id = codigo_estacion) AND (data_time BETWEEN fecha_consulta_first_day AND fecha_consulta_last_day) AND direccion > angle1 AND direccion <= angle2);
    SET valor4 = (SELECT COUNT(*) FROM vw_wind_counter WHERE speedy > 9 AND speedy <= 13 AND (device_id = codigo_estacion) AND (data_time BETWEEN fecha_consulta_first_day AND fecha_consulta_last_day) AND direccion > angle1 AND direccion <= angle2);
    SET valor5 = (SELECT COUNT(*) FROM vw_wind_counter WHERE speedy > 13 AND speedy <= 18 AND (device_id = codigo_estacion) AND (data_time BETWEEN fecha_consulta_first_day AND fecha_consulta_last_day) AND direccion > angle1 AND direccion <= angle2);
    SET valor6 = (SELECT COUNT(*) FROM vw_wind_counter WHERE speedy > 18 AND (device_id = codigo_estacion) AND (data_time BETWEEN fecha_consulta_first_day AND fecha_consulta_last_day) AND direccion > angle1 AND direccion <= angle2);
    
    INSERT INTO viento_count SELECT angle_grf AS angulo, valor1 AS val1, valor2 AS val2, valor3 AS val3, valor4 AS val4, valor5 AS val5, valor6 AS val6;
    
    SET angle = angle + 15;
    
    END LOOP myloop;
    
    SELECT * FROM viento_count;
    DROP TEMPORARY TABLE viento_count;  

目前,我从视图 (vw_wind_counter) 获取数据,其中我 CAST 速度和方向数据为 DECIMAL 值。 我看到了关于这个问题的另一个话题,但其中很多是关于错误的数据插入。

您似乎从未将 angle 变量初始化为 0,因此它未定义。

也就是说,您不需要变量或循环...

只要用一个table你感兴趣的不同角度,然后用条件聚合COUNT()里面有CASE表达式).

INSERT INTO
  viento_count (angulo, val1, val2, val3, val4, val5, val6)
SELECT
  CAST(a.angle AS DECIMAL(4,1)) + 7.5,
  COUNT(CASE WHEN                   w.speedy <= 3  THEN 1 END),
  COUNT(CASE WHEN w.speedy > 3  AND w.speedy <= 6  THEN 1 END),
  COUNT(CASE WHEN w.speedy > 6  AND w.speedy <= 9  THEN 1 END),
  COUNT(CASE WHEN w.speedy > 9  AND w.speedy <= 13 THEN 1 END),
  COUNT(CASE WHEN w.speedy > 13 AND w.speedy <= 18 THEN 1 END),
  COUNT(CASE WHEN w.speedy > 18                    THEN 1 END)
FROM
  vw_wind_counter   AS w
CROSS JOIN
(
  SELECT
    (a*5+b)*15   AS angle
  FROM
    (select 0 a union all select 1 union all select 2 union all select 3 union all select 4) t1,
    (select 0 b union all select 1 union all select 2 union all select 3 union all select 4) t2
  WHERE
    (a*5+b)*15 < 360
)
  AS a
WHERE
      w.device_id = codigo_estacion
  AND w.data_time BETWEEN fecha_consulta_first_day AND fecha_consulta_last_day
  AND w.direccion >  a.angle
  AND w.direccion <= a.angle + 15

注意:子查询只是创建一个 table 的角度,从 0 到 345,增量为 15。

另外:范围传统上是 (>= and <) 而不是 (> and <=)。例如,您当前的代码包括 direccion = 360 不包括 direccion = 0,这是不正常的。