Retrieving First and Last Records of Grouped Data with Aggregate Functions in MySQL
In MySQL, when fetching data from grouped records using aggregate functions, it can be challenging to retrieve the first and last records of the group. Although multiple queries can accomplish this, it can be inefficient for large tables.
To optimize this process, MySQL offers a more efficient solution using the GROUP_CONCAT and SUBSTRING_INDEX functions.
Using GROUP_CONCAT and SUBSTRING_INDEX
By using these functions together, you can concatenate values from the group and then extract the first and last values using SUBSTRING_INDEX. Here's how:
SUBSTRING_INDEX(GROUP_CONCAT(CAST(open AS CHAR) ORDER BY datetime), ',', 1) AS open SUBSTRING_INDEX(GROUP_CONCAT(CAST(close AS CHAR) ORDER BY datetime DESC), ',', 1) AS close
How it Works:
Example:
Consider the following query:
SELECT MIN(low_price), MAX(high_price), SUBSTRING_INDEX(GROUP_CONCAT(CAST(open AS CHAR) ORDER BY datetime), ',', 1) AS open, SUBSTRING_INDEX(GROUP_CONCAT(CAST(close AS CHAR) ORDER BY datetime DESC), ',', 1) AS close FROM symbols WHERE date BETWEEN(.. ..) GROUP BY YEARWEEK(date)
This query efficiently retrieves the first (open) and last (close) records for each group, minimizing processing time for large tables.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3