繁体   English   中英

从数据库中获取每天和每月的总销售额

[英]Fetching Total Sale of each day and month from database

我有c_date列为datetime表, total为mysql中的int类型,我想打印出每一天的销售额,以及每月的总销售额,以及每年的总销售额,包括没有销售额的日,月,年。

当前用于日常销售,我正在运行以下查询:

mysql> select date(c_date) as date, sum(total) as total_sale from sale group by date;
+------------+------------+
| date       | total_sale |
+------------+------------+
| 2013-10-3  |        798 |
| 2013-10-6  |        114 |
+------------+------------+

但是,我想要这样的东西:

mysql> select date(c_date) as date, sum(total) as total_sale from sale group by date;
+------------+------------+
| date       | total_sale |
+------------+------------+
| 2013-10-1  |          0 |
| 2013-10-2  |          0 |
| 2013-10-3  |        798 |
| 2013-10-4  |          0 |
| 2013-10-5  |          0 |
| 2013-10-6  |        114 |
+------------+------------+

而对于每月,我得到这个:

mysql> select c_date, month(c_date) as month, year(c_date) as year, sum(total) as total from sale group by c_date order by c_date;
+---------------------+-------+------+-------+
| c_date              | month | year | total |
+---------------------+-------+------+-------+
| 2013-10-3 02:40:06  |    10 | 2013 |   228 |
| 2013-10-3 02:41:58  |    10 | 2013 |   114 |
| 2013-10-3 02:44:36  |    10 | 2013 |   114 |
| 2013-10-3 02:46:40  |    10 | 2013 |   114 |
| 2013-10-3 02:49:15  |    10 | 2013 |   114 |
| 2013-10-3 02:53:36  |    10 | 2013 |   114 |
| 2013-10-6 07:43:27  |    10 | 2013 |   114 |
+---------------------+-------+------+-------+

但是我想要这样的东西:

mysql> select c_date, month(c_date) as month, year(c_date) as year, sum(total) as total from sale group by c_date order by c_date;
+---------------------+-------+------+-------+
| c_date              | month | year | total |
+---------------------+-------+------+-------+
| 2013-1-3 02:40:06   |     1 | 2013 |     0 |
| 2013-2-3 02:41:58   |     2 | 2013 |     0 |
| 2013-3-3 02:44:36   |     3 | 2013 |     0 |
| 2013-4-3 02:46:40   |     4 | 2013 |     0 |
| 2013-5-3 02:49:15   |     5 | 2013 |     0 |
| 2013-6-3 02:53:36   |     6 | 2013 |     0 |
| 2013-7-6 07:43:27   |     7 | 2013 |     0 |
| 2013-8-3 02:44:36   |     8 | 2013 |     0 |
| 2013-9-3 02:46:40   |     9 | 2013 |     0 |
| 2013-10-3 02:49:15  |    10 | 2013 |   912 |
| 2013-11-3 02:53:36  |    11 | 2013 |     0 |
| 2013-12-6 07:43:27  |    12 | 2013 |     0 |
+---------------------+-------+------+-------+

MysqL有可能吗?

由于不可能在MySQL中使用序列 (实际上, 序列根本不存在),因此您必须首先创建日期范围表。 就像:

CREATE TABLE dates_range (record_date DATE)

然后在此表中填充日期,从sale表中存在的最小日期开始直到最大为止。

之后,使用SQL LEFT JOIN运算符,您将能够像这样聚合数据:

SELECT
  YEAR(dates_range.record_date),
  MONTH(dates_range.record_date),
  DAY(dates_range.record_date),
  COALESCE(SUM(sale.total), 0) AS total_sum
FROM
  dates_range
    LEFT JOIN sale
      ON dates_range.record_date=DATE(sale.c_date)
GROUP BY
  YEAR(dates_range.record_date),
  MONTH(dates_range.record_date),
  DAY(dates_range.record_date)

在我看来,您需要与日历表进行外部联接。

假设日历表的填充方式如下:

日历

Year Month   Day
2013 201310  2013-10-1    
2013 201310  2013-10-2
...

然后您可以编写查询

         select date(c_day) as date, 
                sum(total) as total_sale 
           from calendar c 
left outer join sale s 
             on c.day = s.c_date
          where c.month = 201310
       group by c_day
         having c_day <= max(s.c_date); -- this is to avoid to show all 
                                        -- days for October

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM