Home  >  Article  >  Backend Development  >  How to write SQL to generate daily order quantity and growth statistical reports through the order data table?

How to write SQL to generate daily order quantity and growth statistical reports through the order data table?

WBOY
WBOYOriginal
2016-07-06 13:53:581579browse

For example, I have a database with an order table in it. The table name is orders. This order table has a field add_time, which is the timestamp of adding the order.
I now want to generate a report that displays the daily order quantity and growth, and also needs to include the number of days (Y-m-d format is sufficient, because I will eventually output it to the front-end echarts statistical chart or highcharts statistical chart), how to write the SQL statement for this requirement?
Happy Dragon Boat Festival, thank you for your answer! ~

Reply content:

For example, I have a database with an order table in it. The table name is orders. This order table has a field add_time, which is the timestamp of adding the order.
I now want to generate a report that displays the daily order quantity and growth, and also needs to include the number of days (Y-m-d format is sufficient, because I will eventually output it to the front-end echarts statistical chart or highcharts statistical chart), how to write the SQL statement for this requirement?
Happy Dragon Boat Festival, thank you for your answer! ~

Because we have just completed the framework of an operating system, I would like to provide a basic idea:

  1. Create a new table and put every item of data you need to count, as well as the statistical time, as columns of the new table, like this:

<code class="SQL">    create table analysis (
        today_timestamp varchar(20),
        order_count varchar(20),
        increase_value varchar(20),
        primary key (today_timestamp)
    );</code>
  1. Write a php script, for example, call job.php to get the data you need to count that day: number of orders, growth, etc. For example, if you need the quantity of all orders, just select count(*) from orders. How to write it specifically is another question. After obtaining it, insert it into the table analysis just now. If you want it to execute automatically, consider using crontab.

  2. When you 要输出到前端的echarts统计图或者highcharts统计图, just read the data in this table by date

Use group by statistics. The date field is divided into year, month, day, hour, and minute. Each is a column for statistics. Remember to add indexes and sub-tables.

It is best to provide a sample data structure for your requirement

<code>SELECT
    FROM_UNIXTIME(add_time, "%Y-%m-%d") order_date,
    count(1) AS today_c,
    count(1) - last_c AS change_with_last
FROM
    orders
LEFT JOIN (
    SELECT
        FROM_UNIXTIME(add_time, "%Y-%m-%d") AS last_date,
        count(1) AS last_c
    FROM
        orders
    GROUP BY
        FROM_UNIXTIME(add_time, "%Y-%m-%d")
) last ON (
    FROM_UNIXTIME(temp.add_time, "%Y-%m-%d") = DATE_ADD(
        last.last_date,
        INTERVAL 1 DAY
    )
)
GROUP BY
    FROM_UNIXTIME(add_time);</code>

This should be able to achieve the expected results, but it is best to use a data table to count the daily results, otherwise there will be performance problems

Statement:
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