简体   繁体   中英

More efficient query MYSQL

I have data table like this

Table 1

Field1   Field2 Field3
00:00:01 Test1 10.0
00:00:01 Test2 7.3
00:00:01 Test3 11.03
00:00:10 Test1 11.0
00:00:10 Test2 17.3
00:00:10 Test3 15.03
01:00:01 Test1 5.0
01:00:01 Test2 4.3
01:00:01 Test3 4.03
01:00:10 Test1 5.0
01:00:10 Test2 4.3
01:00:10 Test3 4.03
02:00:01 Test1 78.0
02:00:01 Test2 43.3
02:00:01 Test3 19.03
02:00:10 Test1 79.0
02:00:10 Test2 46.3
02:00:10 Test3 14.03

I need the max value of each hour Iam using the following query but I looking for sometime more efficient to save in other table

INSERT INTO table2 (Time, TYPE, Number)
SELECT Field1, Field2, max(Field3)
  FROM table1
 WHERE Field1 like '00:%' ;
INSERT INTO table2 (Time, TYPE, Number)
SELECT Field1, Field2, max(Field3)
  FROM table1
 WHERE Field1 like '01:%' ;
INSERT INTO table2 (Time, TYPE, Number)
SELECT Field1, Field2, max(Field3)
  FROM table1
 WHERE Field1 like '02:%' ;

..............
..............
to '23:%'

You can use a WHERE NOT EXISTS clause.

SELECT Field1 as hr, Field2 as name, Field3 as num
  FROM table1 orig
 WHERE NOT EXISTS (
    SELECT 1 
      FROM table1 lower 
     WHERE lower.Field3 > orig.Field3
       AND lower.Field1 = orig.Field1
  )

So basically you're saying "Only give me the rows where no other row has the same hour and a higher number" - this will be a very efficient query assuming you have the appropriate indexes.

I'd also recommend you use better names than field1, table1, etc.

I've left with just the SELECT statement - wrap it in an INSERT statement or whatever else you need.

If I understand your query correctly, you want to insert the maximum record with regard to Field3 from each hour appearing in the table1 table. If so, then you can do away with all those insert statements and replace it with a single insert statement. This single statement uses a subquery to identify all the records you want inserted, one for each hour, in a single go.

INSERT INTO table2 (Time, TYPE, Number)
SELECT
    t1.Field1,
    t1.Field2,
    t1.Field3
FROM table t1
INNER JOIN
(
    SELECT HOUR(Field1) AS max_time, MAX(Field3) AS max_field3
    FROM table1
    GROUP BY HOUR(Field1)
) t2
    ON HOUR(t1.Field1) = t2.max_time AND
       t1.Field3       = t2.max_field3

Note that it might be a bit odd to only do an aggregation by the hour if you had multiple dates in your table. Instead, you might want to aggregate using the entire date rounded to the top of the hour.

You need GROUP BY TruncToHour(Field) to make this happen. The trick is implementing TruncToHour() , which you do like this:

   TIME(TIME_FORMAT( Field1, '%H:00:00'))

This takes the time value in each row and strips off the minutes and seconds, replacing them with zero.

Using it, you get this SELECT query ( http://sqlfiddle.com/#!9/a50405/5/0 )

     SELECT TIME(TIME_FORMAT( Field1, '%H:00:00')) Field1, 
            Field2, MAX(Field3) Field3
       FROM table1
      GROUP BY TIME(TIME_FORMAT( Field1, '%H:00:00'), Field2

You can then use that result set in your INSERT statement.

If you are looking to find the detail row from table1 corresponding to the maximum value of of field3 for each hour, you need a nested query using that query above as subquery. Like so. ( http://sqlfiddle.com/#!9/a50405/16/0 )

This uses a query pattern that locates the MAX value in each GROUP BY group, then joins that back to the original table to find the detail row from which the MAX value came.

 SELECT a.Field1, a.Field2, a.Field3
   FROM table1 a
   JOIN (
         SELECT TIME(TIME_FORMAT( Field1, '%H:00:00')) Field1, 
                Field2, MAX(Field3) Field3
           FROM table1
          GROUP BY TIME(TIME_FORMAT( Field1, '%H:00:00')), Field2                        
        ) b ON TIME(TIME_FORMAT( a.Field1, '%H:00:00')) = b.Field1
           AND a.Field2=b.Field2         
           AND a.Field3=b.Field3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM