how to calculate of sum of multiple conditional values in mysql

user3325207

I'm trying to get sums of conditional values and group by minute.

I succeeded with the following query but results takes some time and wonder is there any efficient way?

SELECT x.query, x.value, x.time FROM (
    SELECT id, query, SUM(VALUE) AS value, time FROM `modbuslogs` WHERE query IN ("sensor1","sensor2") AND time LIKE '2020-12-04%' GROUP BY HOUR(time), MINUTE(TIME) 
    UNION
    SELECT id, query, SUM(VALUE) AS value, time FROM `modbuslogs` WHERE query IN ("sensor3","sensor4") AND time LIKE '2020-12-04%' GROUP BY HOUR(time), MINUTE(TIME) 
) x
GROUP BY x.query, HOUR(time), MINUTE(TIME) 
ORDER BY x.id

Table structure:

+--------+-------+-----------------+
|  query | value |       time      |
+--------+-------+-----------------+
|sensor1 |   2   |2012-02-10 00:00 |
|sensor2 |   2   |2012-02-10 00:00 |
|sensor3 |   3   |2012-02-10 00:00 |
|sensor4 |   3   |2012-02-10 00:00 |
|sensor1 |   2   |2012-02-10 00:01 |
|sensor2 |   3   |2012-02-10 00:01 |
|sensor3 |   3   |2012-02-10 00:01 |
|sensor4 |   2   |2012-02-10 00:01 |
+--------+-------+-----------------+

Obtained and expected Output:

+--------+-------+-----------------+
|  query | value |       time      |
+--------+-------+-----------------+
|sensor1 |   4   |2012-02-10 00:00 |
|sensor3 |   6   |2012-02-10 00:00 |
|sensor1 |   5   |2012-02-10 00:01 |
|sensor3 |   5   |2012-02-10 00:01 |
+--------+-------+-----------------+
GMB

There is no point for the union subquery in the first place. This is equivalent to your query:

select id, query, sum(value) as value, min(time) as time
from modbuslogs
where 
    query in ('sensor1', 'sensor2', 'sensor3', 'sensor4')
    and time >= '2020-12-04'
    and time <  '2020-12-05'
group by query, hour(time), minute(time)

This gives one row per query and per minute, with the sum of value. Not that we need an aggregate function around time, so the select clause is consistent with the group by clause. Also, the where clause uses date filtering rather than string matching.

一方、2つの異なる列に2つのセンサーグループを配置する場合は、条件付き集計を使用します。

select id, min(time) as time,
    sum(case when query in ('sensor1', 'sensor2') then value else 0 end) as value_1_2, 
    sum(case when query in ('sensor3', 'sensor4') then value else 0 end) as value_3_4
from modbuslogs
where 
    query in ('sensor1', 'sensor2', 'sensor3', 'sensor4')
    and time >= '2020-12-04'
    and time <  '2020-12-05'
group by hour(time), minute(time)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How calculate the sum of the values in rows and columns?

分類Dev

How to use the mysql sum() function to calculate the sum of each row?

分類Dev

How to create sum of columns in Pandas based on a conditional of multiple columns?

分類Dev

How to create sum of columns in Pandas based on a conditional of multiple columns?

分類Dev

How to calculate new value based on sum of duplicate nominal values in R

分類Dev

MySQL query with multiple random values but sum always within a range

分類Dev

Multiple SUM MYSQL

分類Dev

Is there a way to group values and then calculate sum using RxJava

分類Dev

MySQL: How to sum on joined queries (subqueries) with multiple conditions?

分類Dev

How to calculate time complexity for conditional statement and loops

分類Dev

how calculate a conditional mutate in R with dplyr?

分類Dev

MySQL REPLACE multiple values

分類Dev

Calculate percentage of duplicate values within multiple ranges

分類Dev

How to calculate sum and count in a single groupBy?

分類Dev

How to calculate sum of this series modulo m fast?

分類Dev

How to calculate the sum amount of parent field in Elasticsearch?

分類Dev

How to calculate the accumulated sum over a certain threshold?

分類Dev

How calculate the sum of two DateInterval object

分類Dev

How to calculate sum inside awk command in unix?

分類Dev

How to calculate sum using list comprehension

分類Dev

sum values from multiple ajax requests

分類Dev

Add/sum multiple values in array with same id

分類Dev

sum multiple values by index in two dictionaries

分類Dev

PIVOT/count sum of values in a column - Mysql

分類Dev

MySQL table with multiple values in a field

分類Dev

How to neatly combine multiple conditional statements

分類Dev

How to set a conditional based on multiple models?

分類Dev

How to return multiple values

分類Dev

How to calculate common values across different groups?

Related 関連記事

  1. 1

    How calculate the sum of the values in rows and columns?

  2. 2

    How to use the mysql sum() function to calculate the sum of each row?

  3. 3

    How to create sum of columns in Pandas based on a conditional of multiple columns?

  4. 4

    How to create sum of columns in Pandas based on a conditional of multiple columns?

  5. 5

    How to calculate new value based on sum of duplicate nominal values in R

  6. 6

    MySQL query with multiple random values but sum always within a range

  7. 7

    Multiple SUM MYSQL

  8. 8

    Is there a way to group values and then calculate sum using RxJava

  9. 9

    MySQL: How to sum on joined queries (subqueries) with multiple conditions?

  10. 10

    How to calculate time complexity for conditional statement and loops

  11. 11

    how calculate a conditional mutate in R with dplyr?

  12. 12

    MySQL REPLACE multiple values

  13. 13

    Calculate percentage of duplicate values within multiple ranges

  14. 14

    How to calculate sum and count in a single groupBy?

  15. 15

    How to calculate sum of this series modulo m fast?

  16. 16

    How to calculate the sum amount of parent field in Elasticsearch?

  17. 17

    How to calculate the accumulated sum over a certain threshold?

  18. 18

    How calculate the sum of two DateInterval object

  19. 19

    How to calculate sum inside awk command in unix?

  20. 20

    How to calculate sum using list comprehension

  21. 21

    sum values from multiple ajax requests

  22. 22

    Add/sum multiple values in array with same id

  23. 23

    sum multiple values by index in two dictionaries

  24. 24

    PIVOT/count sum of values in a column - Mysql

  25. 25

    MySQL table with multiple values in a field

  26. 26

    How to neatly combine multiple conditional statements

  27. 27

    How to set a conditional based on multiple models?

  28. 28

    How to return multiple values

  29. 29

    How to calculate common values across different groups?

ホットタグ

アーカイブ