Home Database Mysql Tutorial How to Calculate a Running Total of Daily Order Counts in MySQL?

How to Calculate a Running Total of Daily Order Counts in MySQL?

Jan 22, 2025 am 06:41 AM

How to Calculate a Running Total of Daily Order Counts in MySQL?

MySQL Running Total Calculation: A Practical Example

Working with tabular data often requires calculating running totals for efficient analysis. This example demonstrates how to calculate a running total of daily order counts within MySQL. We start with a query that counts orders per day:

1

2

3

4

5

SELECT DAYOFYEAR(`date`) AS d, COUNT(*) AS daily_count

FROM `orders`

WHERE `hasPaid` > 0

GROUP BY d

ORDER BY d

Copy after login

This yields results similar to:

ddaily_count
205
217
2212
234

To add a running total column, we use a user variable:

1

2

3

4

5

6

7

8

9

10

11

12

13

SET @running_total:=0;

SELECT

    q1.d,

    q1.daily_count,

    (@running_total := @running_total + q1.daily_count) AS running_total

FROM

    (SELECT

        DAYOFYEAR(`date`) AS d,

        COUNT(*) AS daily_count

    FROM `orders`

    WHERE `hasPaid` > 0

    GROUP BY d

    ORDER BY d) AS q1;

Copy after login

This code initializes a variable @running_total to zero. The inner query (q1) selects the day and daily order count. The outer query then iterates through q1, adding each day's count to @running_total and storing the cumulative sum in the running_total column.

The output now includes the running total:

d daily_count running_total
20 5 5
21 7 12
22 12 24
23 4 28

This enhanced query provides a running total, simplifying data analysis and visualization.

The above is the detailed content of How to Calculate a Running Total of Daily Order Counts in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

How to solve the problem of mysql cannot open shared library

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

What is SQLite? Comprehensive overview

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

Run MySQl in Linux (with/without podman container with phpmyadmin)

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

Running multiple MySQL versions on MacOS: A step-by-step guide

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?

See all articles