5

我已经更新了查询,但现在出现的错误是:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 7 行的“AS max_parameter_value FROM tcs.parameter_values_archieve”附近使用正确的语法

    SELECT sum(max_pv) SUM_MAX,
       sum(min_pv) SUM_MIN,
       sum(max_pv) - sum(min_pv) DIFF
  FROM (SELECT t.parameter_value, t.created_date, t.meter_id, parameter_id
          FROM tcs.parameter_values_archieve t
         INNER JOIN (SELECT meter_id,
                           MAX(parameter_value) max_pv AS max_parameter_value
                      FROM tcs.parameter_values_archieve
                     where created_date like '2013-01-01%'
                       and created_date <= '2013-01-01 20:00:00'
                       and meter_id between 1 and 16
                       and parameter_id = 1
                     GROUP BY meter_id) q ON t.meter_id = q.meter_id
                                         AND t.parameter_value =
                                             q.max_parameter_value) MAX_TBL,
       (SELECT t.parameter_value, t.created_date, t.meter_id, parameter_id
          FROM tcs.parameter_values_archieve t
         INNER JOIN (SELECT meter_id,
                           MIN(parameter_value) min_pv AS max_parameter_value
                      FROM tcs.parameter_values_archieve
                     where created_date like '2013-01-01%'
                       and created_date >= '2013-01-01 20:00:00'
                       and meter_id between 1 and 16
                       and parameter_id = 1
                     GROUP BY meter_id) q ON t.meter_id = q.meter_id
                                         AND t.parameter_value =
                                             q.max_parameter_value) MIN_TBL;

这是我为查找最大值而编写的查询。现在我必须总结所有max(parameter_value). 同样,我必须找到min(parameter_value)forcreated_date like '2013-01-01%' and created_date >='2013-01-01 08:00:00'并减去这两个值。

怎么做?我正在使用mysql。

如果我像 foll 一样单独找到 sum,我会得到一个总和,但是当我执行“select sum(min_parameter_value)”时,它会引发字段未知错误。这个总和是否正确?我有疑问吗?

SELECT sum(parameter_value) from

(SELECT t.parameter_value,t.created_date,t.meter_id,parameter_id
    FROM tcs.parameter_values_archieve t
        INNER JOIN (SELECT meter_id, min(parameter_value) AS min_parameter_value
                        FROM tcs.parameter_values_archieve where created_date like'2013-01-07%'and created_date >='2013-01-07 08:00:00'and meter_id between 1 and 16 and parameter_id =1
                        GROUP BY meter_id) q
            ON t.meter_id = q.meter_id
                AND t.parameter_value = q.min_parameter_value)min_tbl;
4

2 回答 2

4

试试这个,

SELECT sum(max_pv) SUM_MAX,
       sum(min_pv) SUM_MIN,
       sum(max_pv) - sum(min_pv) DIFF
  FROM (SELECT t.parameter_value, t.created_date, t.meter_id, parameter_id
          FROM tcs.parameter_values_archieve t
         INNER JOIN (SELECT meter_id,
                           MAX(parameter_value) max_pv AS max_parameter_value
                      FROM tcs.parameter_values_archieve
                     where created_date like '2013-01-01%'
                       and created_date <= '2013-01-01 20:00:00'
                       and meter_id between 1 and 16
                       and parameter_id = 1
                     GROUP BY meter_id) q ON t.meter_id = q.meter_id
                                         AND t.parameter_value =
                                             q.max_parameter_value) MAX_TBL,
       (SELECT t.parameter_value, t.created_date, t.meter_id, parameter_id
          FROM tcs.parameter_values_archieve t
         INNER JOIN (SELECT meter_id,
                           MIN(parameter_value) min_pv AS max_parameter_value
                      FROM tcs.parameter_values_archieve
                     where created_date like '2013-01-01%'
                       and created_date >= '2013-01-01 20:00:00'
                       and meter_id between 1 and 16
                       and parameter_id = 1
                     GROUP BY meter_id) q ON t.meter_id = q.meter_id
                                         AND t.parameter_value =
                                             q.max_parameter_value) MIN_TBL;
于 2013-01-11T05:55:53.340 回答
3
SELECT SUM(CASE WHEN t.parameter_value = q.max_parameter_value THEN t.parameter_value END), 
       SUM(CASE WHEN t.parameter_value = q.min_parameter_value THEN t.parameter_value END),
       SUM(CASE WHEN t.parameter_value = q.max_parameter_value THEN t.parameter_value END) - 
       SUM(CASE WHEN t.parameter_value = q.min_parameter_value THEN t.parameter_value END) AS substract
FROM tcs.parameter_values_archieve t INNER JOIN
 (SELECT meter_id, MAX(CASE WHEN created_date <= '2013-01-01 20:00:00' THEN parameter_value END) AS max_parameter_value, 
                   MIN(CASE WHEN created_date >= '2013-01-01 08:00:00' THEN parameter_value END) AS min_parameter_value 
  FROM  parameter_values_archieve
  WHERE created_date like '2013-01-01%' and meter_id between 1 and 16 and parameter_id = 1
  GROUP BY meter_id) q ON t.meter_id = q.meter_id

简单示例SQLFiddle

于 2013-01-11T08:01:09.977 回答