MySQL select AVG, ORDER BY, GROUP BY & LIMIT -
Statement statement does not work, but I have to find out why
average Choose (___________________US_SEConds);
I want to get the average of the most recent 1000 rows for each line type. created_at type DATETIME and ROW_TYPE type of VARCHAR is
If you want only the 1000 most recent rows Regardless of Row_type, and then get the average of delay_in_seconds for each row_type, which is quite a straightforward question. For example:
select t.row_type, AVG (t.delay_in_seconds) from (select r.row_type, by r.created_at DESC r from 1000 A_table R command LIMIT. Delay_in_seconds) T group by T I know that this question does not meets the requirements specified. (I know that it does not satisfy what I had understood as a specification.) If we want each the most recent 1000 rows for row_type , Whatever will be quite straightforward ... if we are using a database that supports analytical functions. Unfortunately, MySQL does not provide support for analytical functions. But it is possible to simulate one in MySQL, but the syntax is slightly involved, and it is up to the behavior that guarantees not . As an example:
s.row_type, AVG (s.delay_in_seconds) Select (@row_: = IF (@prev_row_type = t.row_type, @row_ + 1, 1) as row_, @prev_row_type: = t. as ROW_TYPE ROW_TYPE, A_table T Cross t.delay_in_seconds JOIN (select @prev_row_type: = zero, @row_: = null) I ordered by t.row_type DESC, DESC t.created_at) from here s.row_ & lt; S.row_type by 1000 group =
Notes:
Inline view query is expensive for a large set. What is effectively doing is specifying a row number in each row, sorting by "sequence by" in sequential order is sorted by created_at
, what we want, we assign the value of 1 The most recent row to go, the next most recent 2, etc. This number will be repeated for a different value for each of the rows ROW_TYPE
.
Our main columns for display (ROW_TYPE, created_at, delay_seconds) want a suitable index with / code> to avoid using an expensive "fileort" operation. We have it at least includes those previously required two columns, which Deri_sekend, it may be satisfied with a covered index (query completely index.) The outer query then view The query runs back against the result (a "derived table") where the predicate all the rows have been assigned a line number greater than 1000 out of the filter, the rest is a straighforward group gate And average gross.
A boundary section is completely unnecessary. It may be possible to add some additional action to increase some extra performance ... like if we have specified the most recent 1000 rows, but were created only within 30 or 90 days?
(I am not completely convinced that OP was asking the answer to the question. What is the answer: Is there a question which is AVG
gross and GROUP BY
, ORDER BY
and LIMIT
section.)
NB This query on the behavior of the MySQL user Dependent - Defined variables that are not guaranteed.
The above question shows an approach, but another approach is also to appoint a row number (a number of rows that are "recently" from each line) It is possible to use the "included" operation (A_table with A_table). However, with big sets, if we are not careful to limit it, then the monstrous intermediate results.
Comments
Post a Comment