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 6245523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:31:18+00:00 2026-05-24T12:31:18+00:00

I have such table in my MySQL database: ————————— |fid | price | date

  • 0

I have such table in my MySQL database:

---------------------------
|fid | price | date       |
---------------------------
|  1 | 1.23  | 2011-08-11 |
|  1 | 1.43  | 2011-08-12 |
|  1 | 1.54  | 2011-08-13 |
|  1 | 1.29  | 2011-08-14 |
|  1 | 1.60  | 2011-08-15 |
|  1 | 1.80  | 2011-08-16 |

fid – this is the product id
price – this is the price of the product in specified day

I want to calculate average price of the product fid=1. I want to calculate the average price of first n=3 rows ordered by date for specified fid, and then calculate average price for another 3 rows ordered by date.

How can I group first 3 rows and calculate avg and then group next 3 rows and calculate avg. Before calculation I need to sort the rows by date and then group n rows.

If n=3 this should return such result:

--------------
|fid | price |
--------------
|  1 | 1.40  | 2011-08-11 -> 2011-08-13 - average price for 3 days
|  1 | 1.56  | 2011-08-14 -> 2011-08-16 - average price for 3 days

How can I create SQL Query to do such calculations?

Thanks in advance.

  • 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-05-24T12:31:18+00:00Added an answer on May 24, 2026 at 12:31 pm

    Unluckily mysql doesn’t offer analytic functions like oracle,mssql and postgres do. So you have to play with variables to reach your goal.

    create table mytest (
    id int not null auto_increment primary key,
    fid int,
    price decimal(4,2),
    fdate date
    ) engine = myisam;
    
    insert into mytest (fid,price,fdate)
    values 
    (1,1.23,'2011-08-11'),
    (1,1.43,'2011-08-12'),
    (1,1.54,'2011-08-13'),
    (1,1.29,'2011-08-14'),
    (1,1.60,'2011-08-15'),
    (1,1.80,'2011-08-16');
    
    
    select 
    concat_ws('/',min(fdate),max(fdate)) as rng,
    format(avg(price),2) as average from (
    select *,@riga:=@riga+1 as riga
        from mytest,(select @riga:=0) as r order by fdate
         ) as t
    group by ceil(riga/3);
    
    
    +-----------------------+---------+
    | rng                   | average |
    +-----------------------+---------+
    | 2011-08-11/2011-08-13 | 1.40    |
    | 2011-08-14/2011-08-16 | 1.56    |
    +-----------------------+---------+
    2 rows in set (0.02 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this kind of table on my Database MySql : CREATE TABLE `users`
I have two tables in my MySQL database (table1 and table 2). I want
We have a mySQL database table for products . We are utilizing a cache
Hi I have a MySQL database table points the user can click a button
In our MySQL database, I have a third_party_accounts table and it has_many third_party_campaigns .
I have a website where I display information from a mysql table such as
If I have a string that represents the date and time such as '30/09/2011
I have a table with a field which contains strings in my MySQL database.
In a mysql database I have table of geographical areas where each row has
I have a MySql database that holds datetime in one field, in this format:

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.