Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8381415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:41:40+00:00 2026-06-09T16:41:40+00:00

i have a table like this from | to | value ———————– 08:10 |

  • 0

i have a table like this

from   | to     | value
-----------------------
08:10  | 08:12  | 2
08:13  | 08:20  | 5
08:30  | 08:45  | 3
08:46  | 08:55  | 1

and i need to group by intervals of say 1, 10, 60 minutes and get the average value, assuming each minute has one value.

Note: there are holes between the ranges and from is inclusive, to non inclusive

from   | to     | average_value
-----------------------
08:10  | 08:19  | 4
08:20  | 08:29  | 5
08:30  | 08:39  | 3
08:40  | 08:49  | 2
08:50  | 08:59  | 1

My idea was to use a stored procedure to transform the entries to

time   | value
-----------------------
08:10  | 2
08:11  | 2
08:12  | 2
08:13  | 5
...
08:20  | 5
08:30  | 3
...
08:45  | 3
08:50  | 1
...
08:55  | 1

my first attempt

DELIMITER $$
CREATE PROCEDURE Decompose()
BEGIN
    DECLARE num_rows INT;
    DECLARE from DateTime;
    DECLARE to DateTime;
    DECLARE value double;

    DECLARE friends_cur CURSOR FOR SELECT from, to, value FROM sourcetable;

    -- 'open' the cursor and capture the number of rows returned
    -- (the 'select' gets invoked when the cursor is 'opened')
    OPEN friends_cur;
    select FOUND_ROWS() into num_rows;

    WHILE num_rows > 0 DO

        FETCH friends_cur INTO  from, to, value;

        SELECT from, value;

        SET num_rows = num_rows - 1;
    END WHILE;

    CLOSE friends_cur;
END$$
DELIMITER ;

leads to ‘Command out of sync’ error because each SELECT in the loop will create a new resultset everytime instead of returning a row.

Questions:

  • can i make the stored procedure work somehow?
  • is there a sql possible to get the average value for each interval?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:41:42+00:00Added an answer on June 9, 2026 at 4:41 pm

    I think your fundamental plan is sound.

    I would start by creating a numbers table. ( Creating a "Numbers Table" in mysql )

    From that, convert that into a table with rows for each minute of the day for which you wish to calculate the average values – including those that don’t have any value – and a column to indicate which interval that particular minute belongs to

    Time     Interval
    08:00    1
    08:01    1
    08:02    1
    08:03    1
    08:04    1
    08:05    2
    08:06    2
    ....
    

    And from that left join to your sample table using

    ON intervals.Time >= Samples.Time and intervals.Time<Sample.Time
    

    to produce a query of the intervals, then do the average against that.

    I don’t think you need the cursor.

    (I would give more concrete examples, but I lack knowledge of some of MySql variety of SQL intricacies)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an table structure like this mysql> SELECT id, name, parent_id FROM categories;
I have a table which looks like this: mysql> SHOW COLUMNS FROM Users; +------------+--------------+------+-----+---------+----------------+
I like to have a query like this: select data from table where (x
I have a select like this: SELECT field1, field2, field3 FROM table WHERE field1=
i have this sql query select * from table where name like ? but
i have a table like this NAME VALUE ----------------- bla 1 bla 2 bla
I have a table that looks like something like this: timestamp value person ===============================================
I have a table like this: +-----+-----+-------+ | id | fk | value |
I have a table that looks like this: id datatype name value 0 nvarchar(255)
I have a table that seems like this: +-----+-----------+------------+ | id | value |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.