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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:30:02+00:00 2026-06-18T06:30:02+00:00

I have searched a lot, but cannot find a helpful answer: i want to

  • 0

I have searched a lot, but cannot find a helpful answer:

i want to have a list of totals from a period the user defines by giving me a start and end date. The totals should every time being from the start date to beginning with the start date and add every row 1 day. so the last row gives the totals from start to end date.
example: – given period = start 2013-01-01 , end = 2013-01-31

total day 1 = 100
total day 2 = 0 (not listed in my totalsperday query, but should have a row in my final query)
total day 3 = 140
total day 4 = 20
...

final table should look like:
end day 1: 100
end day 2: 100
end day 3: 240
end day 4: 260
...

so i have a query who calculates all days:

SELECT '2013-01-01' as startdate, w.endDate
FROM
(
    SELECT date('2013-01-01' + INTERVAL u.i*100 + v.i*10 + w.i DAY) AS endDate
    FROM sysints AS u
    JOIN sysints AS v
    JOIN sysints AS w
    WHERE ( u.i*100 + v.i*10 + w.i ) <= 
    (
        SELECT DATEDIFF( '2013-01-31','2013-01-01') as ddff
    )
) w
ORDER BY w.endDate ASC

and i have a query who calculates the totals per day

SELECT p.selldate, SUM(p.price) as totalPerDay
FROM products p
WHERE   '2013-01-01' >= p.selldate <= '2013-01-31'
GROUP BY p.selldate
ORDER BY p.selldate ASC

now combining these two to get my final result is hard.

basically what the final query should look like is:

- make the sum of sumperday from day 1 to day 1
- make the sum of sumperday from day 1 to day 2
- make the sum of sumperday from day 1 to day 3 
...

any help?
thx.
this is a simplified example of my final query.

  • 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-18T06:30:04+00:00Added an answer on June 18, 2026 at 6:30 am

    Below is the sample. The idea is to obtain a initial data set ordered by date and having aggregate totals, implicit date range records. Then using the cursor you can pass through each row to get the final total column (amountCalc column in the sample) just by summarize the previous records – will work because you already have the columns ordered by date.

    The procedure can have other input/ output parameters. Instead of getting info from table you can get data from one view, where the view can be already order by date asc. Is just a sample so can be customized as needed.

    Good luck.

    -- drop table `Balance`;
    CREATE TABLE `Balance` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `date` DATE NOT NULL,
      `account` varchar(30) NOT NULL,
      `amount` DECIMAL(10,2) NOT NULL, 
      PRIMARY KEY (`id`)
    );
    
    INSERT INTO `Balance` (`date`, `account`, `amount`) VALUES 
    ('2013-01-02', 'T355176', 8700), 
    ('2013-01-03', 'T355176', 8900), 
    ('2013-01-04', 'T355215', 33308), 
    ('2013-01-03', 'T355215', 116581), 
    ('2013-01-06', 'T812022', 275000), 
    ('2013-01-02', 'T812063', 136500), 
    ('2013-01-05', 'T812063', 11682), 
    ('2013-01-06', 'T812064', 615100), 
    ('2013-01-03', 'T812064', 25000), 
    ('2013-01-02', 'T812085', 82500);
    
    
    SELECT * FROM Balance WHERE date >= '2013-01-01' AND date <= '2013-01-06' ORDER BY date ASC;
    CALL sp_getTotals('2013-01-01', '2013-01-06');
    
    
    -- --------------------------------------------------------------------------------
    -- Routine DDL
    -- Note: comments before and after the routine body will not be stored by the server
    -- --------------------------------------------------------------------------------
    DELIMITER $$
    
    CREATE DEFINER=`root`@`%` PROCEDURE `sp_getTotals`(IN startDate DATE, IN endDate DATE)
    BEGIN
    
    
    DECLARE dt DATE;
    DECLARE amt DECIMAL(10,2);
    DECLARE amtCalcPart DECIMAL(10,2);
    DECLARE done INT DEFAULT 0;
    
    DECLARE dtStart DATE;
    DECLARE dtEnd DATE;
    
    DECLARE cur1 CURSOR FOR SELECT date, amount FROM `TempMB`;
    DECLARE cur2 CURSOR FOR SELECT startDate, endDate;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    
    
    DROP TEMPORARY TABLE IF EXISTS `TempMB`;
    CREATE TEMPORARY TABLE IF NOT EXISTS `TempMB` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
      `date` DATE NOT NULL,
      `amount` DECIMAL(10,2) NULL DEFAULT 0.00,
      `amountCalc` DECIMAL(10,2) NULL DEFAULT 0.00,
      PRIMARY KEY (`id`)
    );
    
    
    SET dtStart = DATE(startDate);
    SET dtEnd = DATE(endDate);
    
    WHILE dtStart <= dtEnd DO
        INSERT INTO `TempMB` (`date`) SELECT dtStart;
        SET dtStart = DATE_ADD(dtStart, INTERVAL 1 DAY);
    END WHILE;
    
    
    SELECT * FROM TempMB;
    
    -- Fill temp table with info needed
    UPDATE `TempMB` t 
    INNER JOIN 
    (
        SELECT date, SUM(amount) AS amount
        FROM Balance 
        WHERE 
            date >= startDate AND date <= endDate 
        GROUP BY date
        ORDER BY date ASC
    ) b ON b.date = t.date 
    SET 
        t.amount = b.amount;
    /*INSERT INTO `TempMB` (`date`, `amount`)
    SELECT date, SUM(amount) AS amount
    FROM Balance 
    WHERE 
        date >= startDate AND date <= endDate 
    GROUP BY date
    ORDER BY date ASC;
    */
    
    
    SET amtCalcPart = 0.00;
    -- Initialise cursor
    OPEN cur1;
    -- USE BEGIN-END handler for cursor-control within own BEGIN-END block
    BEGIN
    DECLARE EXIT HANDLER FOR NOT FOUND BEGIN END;
    -- Loop cursor throu temp records
    LOOP 
        -- Get next value
        FETCH cur1 INTO dt, amt;
    
        -- Calculate amountCalc
        SET amtCalcPart = (SELECT SUM(amount) as amt FROM `TempMB` WHERE Date <= dt);
        UPDATE `TempMB` SET amountCalc = amtCalcPart WHERE date = dt;
    
    END LOOP;
    END;
    
    -- Release cursor
    CLOSE cur1;
    
    SELECT * FROM TempMB;
    
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched a lot but cannot find the information i want. I have
I have searched a lot of threads but i can't find any answer that
I have searched a lot and tried much but I can not find the
I have searched a lot but i did not get any solution. I want
i have searched a lot but could not find the exact way to do
I have searched this site extensively but cannot find a solution. Here is the
I have searched a lot but I could not find any good solution to
I read on here and searched a lot but didn't find the answer, so
i have searched a lot on the web but can not find the actual
I have searched a lot but i could not find any way to convert

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.