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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:11:31+00:00 2026-06-01T09:11:31+00:00

I want to count payment within selected date, but i can’t figure it out

  • 0

I want to count payment within selected date, but i can’t figure it out how to do it.

Here is the example data from the my table

id    starts_from     payment_per_day
=======================================
1     2012-01-01      10,000.00
2     2012-01-15      10,500.00
3     2012-02-01      11,000.00
4     2012-02-15      11,500.00
5     2012-03-01      12,000.00

How do i count total payment from 2012-01-21 to 2012-02-20 ?

The total payment should be 338,500

from 2012-01-21 to 2012-01-31 = 11 days * 10,500
from 2012-02-01 to 2012-02-14 = 14 days * 11,000
from 2012-02-15 to 2012-02-20 =  6 days * 11,500

But if i do :

SELECT SUM(payment_per_day) as total FROM table 
WHERE starts_from BETWEEN '2012-01-21' AND '2012-02-20'

the result is only 22,500

Any ideas ?

  • 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-01T09:11:32+00:00Added an answer on June 1, 2026 at 9:11 am

    I would first expand the range into the list of dates, then use the following query:

    SELECT SUM(p1.payment_per_day)
    FROM dates d
      INNER JOIN payments p1 ON p1.starts_from <= d.date
      LEFT  JOIN payments p2 ON p2.starts_from <= d.date
                            AND p2.starts_from > p1.starts_from
    WHERE p2.id IS NULL
    

    You could obtain the list from the range with the help of a numbers table, like this:

    SELECT DATE_ADD(@date_from, INTERVAL num DAY)
    FROM numbers
    WHERE num BETWEEN 0 AND DATEDIFF(@date_to, @date_from)
    

    A numbers table is a thing worth having as it can be useful in many situations, so consider providing yourself with one. Here’s a very simple script to create and initialise a numbers table:

    CREATE TABLE numbers AS SELECT 0 AS num;
    SET @ofs = (SELECT COUNT(*) FROM numbers); INSERT INTO numbers SELECT @ofs + num FROM numbers;
    SET @ofs = (SELECT COUNT(*) FROM numbers); INSERT INTO numbers SELECT @ofs + num FROM numbers;
    SET @ofs = (SELECT COUNT(*) FROM numbers); INSERT INTO numbers SELECT @ofs + num FROM numbers;
    … /* repeat as necessary, each line doubles the number of rows in the table */
    

    But, of course, you can use a loop instead.

    Here’s my complete testing environment on SQL Fiddle (for anyone to play with).

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

Sidebar

Related Questions

I want to count how many checkboxes a user has selected. For example, from
I want to count the rows in several date ranges (i.e: last hour, today,
I want to count the number of accounts from the resulting table generated from
i want to count how many numbers from 0 to 9 is in string.
I want to count all the select list where its value selected as 1.
I want to spit out everything in Request.Form so I can just return it
I want to count the vehicles from a video. After frame differencing I got
I want to count distinct values in a column. Column contains data like this
I want to count how many times each user has rows within '5' of
I have the following table structure and i want count all room types from

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.