在sql脚本中一起使用sum和max

时间:2013-01-11 05:43:23

标签: mysql sql database

我已更新了查询,但现在出现的错误是:您的SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以便在'AS max_parameter_value附近使用正确的语法                       来自第7行的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)找到created_date like '2013-01-01%' and created_date >='2013-01-01 08:00:00'并减去这两个值。

怎么做?我正在使用mysql。

如果我单独找到总和像foll一样,我得到一笔钱,但是当我做“选择总和(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;

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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