简体   繁体   English

MySQL sum() 按月和年的值每次都会给我不同的结果

[英]MySQL sum() values by month and year gives me a different result each time

I'm trying to compute the monthly sum of daily data (with a date format of %Y-%m-%d) over the years of customer "scores", but each time I run my query into a new table I get a different sum result, with random dates getting skipped it seems.我正在尝试计算多年来客户“分数”的每日数据的每月总和(日期格式为 %Y-%m-%d),但是每次我将查询运行到一个新表中时,我都会得到一个不同的总和结果,似乎跳过了随机日期。

Looks like the issue are the customer names.看起来问题是客户名称。

My current query:我目前的查询:

INSERT INTO monthscore
(SELECT name, YEAR(date) as year, MONTH(date) as month, sum(score) as monthscore 
FROM fastfoodSales
GROUP BY name, year, month)

So for example, a customer has an actual score in a month of 400,000, but when I run the above query it outputs 380,000 or 399,000 or 390,000.例如,客户在一个月内的实际分数为 400,000,但是当我运行上述查询时,它输出 380,000 或 399,000 或 390,000。 There are about 100,000 customers with data spanning since 1960.自 1960 年以来,约有 100,000 名客户的数据跨越。

Example of the fastfoodSales data:快餐销售数据示例:

+----+---------+------------+--------+
| id | name    | date       | score  |
+----+---------+------------+--------+
| 1  | Anthony | 2018-12-15 | 15.81  |
+----+---------+------------+--------+
| 2  | Jason   | 2018-12-15 | 123.12 |
+----+---------+------------+--------+
| 3  | Chris   | 2019-11-17 | 18.21  |
+----+---------+------------+--------+
| 4  | Chris   | 2019-11-17 | 19.24  |
+----+---------+------------+--------+
| 5  | Chris   | 2019-11-18 | 24.12  |
+----+---------+------------+--------+
| 6  | Jason   | 2019-12-01 | 14.24  |
+----+---------+------------+--------+
| 7  | Jason   | 2019-12-04 | 14.24  |
+----+---------+------------+--------+
| 8  | Anthony | 2019-12-14 | 81.14  |
+----+---------+------------+--------+
| 9  | Anthony | 2019-12-15 | 23.21  |
+----+---------+------------+--------+


CREATE TABLE `fastfoodSales` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `date` date DEFAULT NULL,
  `score` double(18,2) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `indApril1` (`date`),
  KEY `indnov29` (`name`),
  KEY `indascore` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=4152675 DEFAULT CHARSET=latin1

Expected output into monthscore table:预期输出到monthscore表中:

+---------+------+-------+------------+
| name    | year | month | monthscore |
+---------+------+-------+------------+
| Anthony | 2019 | 08    | 3213.23    |
| Anthony | 2019 | 09    | 12312.32   |
| Anthony | 2019 | 10    | 531.38     |
| Anthony | 2019 | 11    | 431.17     |
| Anthony | 2019 | 12    | 4314.34    |
| Jason   | 2007 | 01    | 39.12      |
| Jason   | 2007 | 02    | 2.1        |
| Jason   | 2007 | 03    | 14.4       |
| Jason   | 2007 | 04    | 23.20      |
+---------+------+-------+------------+

CREATE TABLE `monthscore` (
  `name` varchar(200) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `year` int(4) DEFAULT NULL,
  `month` int(2) DEFAULT NULL,
  `score` double(19,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1

But I end up getting:但我最终得到:

+---------+------+-------+------------+
| name    | year | month | monthscore |
+---------+------+-------+------------+
| Anthony | 2019 | 08    | 2213.23    |
| Anthony | 2019 | 09    | 11312.32   |
| Anthony | 2019 | 10    | 531.38     |
| Anthony | 2019 | 11    | 431.17     |
| Anthony | 2019 | 12    | 4314.34    |
| Jason   | 2007 | 01    | 1.12       |
| Jason   | 2007 | 02    | 2.1        |
| Jason   | 2007 | 03    | 14.4       |
| Jason   | 2007 | 04    | 18.17      |
+---------+------+-------+------------+

Your query seems fine - but I don't know why you would want to store derived data...您的查询看起来不错-但我不知道您为什么要存储派生数据...

DROP TABLE IF EXISTS  fastfoodSales;

CREATE TABLE fastfoodsales
(id SERIAL PRIMARY KEY
,name  VARCHAR(12) NOT NULL
,date  DATE NOT NULL
,score DECIMAL(6,2) NOT NULL
);

INSERT INTO fastfoodSales (name,date,score) VALUES
('Anthony','2018-12-15',15.81),
('Jason','2018-12-15',123.12),
('Chris','2019-11-17',18.21),
('Chris','2019-11-17',19.24),
('Chris','2019-11-18',24.12),
('Jason','2019-12-01',14.24),
('Jason','2019-12-04',18.19),
('Anthony','2019-12-14',81.14),
('Anthony','2019-12-15',23.21);

SELECT name
     , DATE_FORMAT(date,'%Y-%m') yearmonth
     , SUM(score) monthscore 
  FROM fastfoodSales
 GROUP 
    BY name
     , yearmonth

+---------+-----------+------------+
| name    | yearmonth | monthscore |
+---------+-----------+------------+
| Anthony | 2018-12   |      15.81 |
| Anthony | 2019-12   |     104.35 |
| Chris   | 2019-11   |      61.57 |
| Jason   | 2018-12   |     123.12 |
| Jason   | 2019-12   |      32.43 |
+---------+-----------+------------+

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

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