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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:50:15+00:00 2026-05-30T11:50:15+00:00

I have a table orders like this: customer_id order_date 10 2012-01-01 11 2012-01-02 10

  • 0

I have a table orders like this:

customer_id  order_date

10           2012-01-01
11           2012-01-02
10           2012-01-02
12           2012-01-03
11           2012-01-04

12           2012-02-01
11           2012-02-04
13           2012-02-05
14           2012-02-06

How can I get a cumulative average over time (per month) like this:

order date   count orders    count customers   (customer_id)
2012-01      1               1                 (12)
2012-01      2               2                 (10,11)
2012-02      1               2                 (13,14)
2012-02      2               2                 (10,12
2012-02      3               2                 (11)

showing how the number of customers vs. number of orders per customer develops over time.

The following query gives me the wanted information – but not over time. How can I iterate the query over time?

SELECT number_of_orders, count(*) as amount FROM (
 SELECT o.customer_id, count(*) as number_of_orders
   FROM orders o
 GROUP BY o.customer_id) as t1
GROUP BY number_of_orders

Update:

have now build the following PHP code to generate what I need, wonder if that could be done using cumulative counts like on http://www.freeopenbook.com/mysqlcookbook/mysqlckbk-chp-12-sect-14.html

    $year = 2011;
    for ($cnt_months = 1; $cnt_months <= 12; $cnt_months++) {
        $cnt_months_str = ($cnt_months < 10) ? '0'.$cnt_months : $cnt_months;
        $raw_query = "SELECT number_of_orders, count(*) as amount
              FROM (
                SELECT
                o.customer_id,
                count(*) as number_of_orders
                FROM orders o
                 where Date_Format( o.order_date, '%Y%m' ) >= " . $year . "01 and Date_Format( o.order_date, '%Y%m' ) <= " . $year . $cnt_months_str . "
                GROUP BY o.customer_id) as t1
              GROUP BY number_of_orders";

        $query = db_query($raw_query);
        while ($row = db_fetch_array($query)) {
            $data[$cnt_months_str][$row['number_of_orders']] = array($row['number_of_orders'], (int)$row['amount']);

        }
    }
  • 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-30T11:50:16+00:00Added an answer on May 30, 2026 at 11:50 am

    A good starting point is

    SELECT
        order_date,
        COUNT(*) AS distinctOrders,
        COUNT(DISTINCT customer_id) AS distinctCustomers,
        GROUP_CONCAT(DISTINCT customer_id ASC) AS customerIDs
    FROM orders
    
    GROUP BY order_date ASC
    

    This will give you the order_date, the number of orders on that date, the number of customers on that date, and the list of customer ids on that date.

    Just looking at a way to tally up on a month by month basis. So taking this forward I’ve used a subquery to tally up as it goes

    SELECT 
        ordersPerDate.*,
        IF(
            MONTH(ordersPerDate.order_date)=@thisMonth,
            @runningTotal := @runningTotal+ordersPerDate.distinctOrders,
            @runningTotal := 0
        ) AS ordersInThisMonth,
        @thisMonth := MONTH(ordersPerDate.order_date)
    FROM
    (
        SELECT
        @thisMonth := 0,
        @runningTotal := 0
    ) AS variableInit,
    (
        SELECT
            order_date,
            COUNT(*) AS distinctOrders,
            COUNT(DISTINCT customer_id) AS distinctCustomers,
            GROUP_CONCAT(DISTINCT customer_id ASC) AS customerIDs
        FROM orders
    
        GROUP BY order_date ASC
    ) AS ordersPerDate
    

    And finally to clean it up, wrapped it in yet another subquery just to return the rows desired rather than the internal variables

    Grouping on individual days

    SELECT 
        collatedData.order_date,
        collatedData.ordersInThisMonth AS count_orders,
        collatedData.distinctCustomers AS count_customers,
        collatedData.customerIDs AS customer_ids
    FROM (
    
        SELECT 
            ordersPerDate.*,
            IF(
                MONTH(ordersPerDate.order_date)=@thisMonth,
                @runningTotal := @runningTotal+ordersPerDate.distinctOrders,
                @runningTotal := 0
            ) AS ordersInThisMonth,
            @thisMonth := MONTH(ordersPerDate.order_date)
        FROM
        (
            SELECT
            @thisMonth := 0,
            @runningTotal := 0
        ) AS variableInit,
        (
            SELECT
                order_date,
                COUNT(*) AS distinctOrders,
                COUNT(DISTINCT customer_id) AS distinctCustomers,
                GROUP_CONCAT(DISTINCT customer_id) AS customerIDs
            FROM orders
    
            GROUP BY order_date ASC
        ) AS ordersPerDate
    ) AS collatedData
    

    And now finally, following additional information from the OP, the end product

    Grouping on calendar months

    // Top level will sanitise the output
    SELECT 
        collatedData.orderYear,
        collatedData.orderMonth,
        collatedData.distinctOrders,
        collatedData.ordersInThisMonth AS count_orders,
        collatedData.distinctCustomers AS count_customers,
        collatedData.customerIDs AS customer_ids
    FROM (
        // This level up will iterate through calculating running totals
        SELECT 
            ordersPerDate.*,
            IF(
                (ordersPerDate.orderYear,ordersPerDate.orderMonth) = (@thisYear,@thisMonth),
                @runningTotal := @runningTotal+ordersPerDate.distinctOrders*ordersPerDate.distinctCustomers,
                @runningTotal := 0
            ) AS ordersInThisMonth,
            @thisMonth := ordersPerDate.orderMonth,
            @thisYear := ordersPerDate.orderYear
        FROM
        (
            SELECT
            @thisMonth := 0,
            @thisYear := 0,
            @runningTotal := 0
        ) AS variableInit,
        (
            // Next level up will collate this to get per year, month, and per number of orders
            SELECT
                ordersPerDatePerUser.orderYear,
                ordersPerDatePerUser.orderMonth,
                ordersPerDatePerUser.distinctOrders,
                COUNT(DISTINCT ordersPerDatePerUser.customer_id) AS distinctCustomers,
                GROUP_CONCAT(ordersPerDatePerUser.customer_id) AS customerIDs
            FROM (
                // Inner query will get the number of orders for each year, month, and customer
                SELECT
                    YEAR(order_date) AS orderYear,
                    MONTH(order_date) AS orderMonth,
                    customer_id,
                    COUNT(*) AS distinctOrders
                FROM orders
                GROUP BY orderYear ASC, orderMonth ASC, customer_id ASC
            ) AS ordersPerDatePerUser
            GROUP BY  
                ordersPerDatePerUser.orderYear ASC,  
                ordersPerDatePerUser.orderMonth ASC, 
                ordersPerDatePerUser.distinctOrders DESC
        ) AS ordersPerDate
    
    
    ) AS collatedData
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an orders table with a schema like this. CREATE TABLE orders (
In sqlalchemy 0.5 i have a table defined like this one: orders = Table('orders',
If I have an items_ordered table that looks like this: items_ordered customerid order_date item
I am writing a turbogears2 application. I have a table like this: class Order(DeclarativeBase):
I have a customers table, and an orders table. Each customer can have many
I have a table of customers, orders, and items. I'd like to write a
I'm trying to find duplicate customers in a table that looks like this: customer_id
I have a table like: I have to order the rows in function of
I have a table that looks like: Client_ID | Order_ID | 10 | 1
I have a db table containing a column display_order . The data looks like

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.