Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sum and max together in sql script

I have updated the query ,but now the error coming is :You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS max_parameter_value FROM tcs.parameter_values_archieve' at line 7

    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;

This is the query that I have written to find the max. Now I have to sum all the max(parameter_value). Similarly I have to find the min(parameter_value) for created_date like '2013-01-01%' and created_date >='2013-01-01 08:00:00' and subtract these two values.

How to do that?I am using mysql.

If i find sum seperately like foll ,i get a sum but when i do "select sum(min_parameter_value) "it throws field unknown error .Is this sum correct ?i have a doubt?

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;
like image 298
user1896796 Avatar asked Jan 11 '13 05:01

user1896796


1 Answers

Try this,

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;
like image 123
Orangecrush Avatar answered Sep 22 '22 00:09

Orangecrush