我想每个月计算评估分数。评估范围从1-5。我想在表格中向他们展示每个评估每月评分的分数。这是数据。日期列的格式为:YYYY-MM-DD。
+-----------+---+---+---+---+---+
| date/eval | 5 | 4 | 3 | 2 | 1 |
+-----------+---+---+---+---+---+
|2015-06-01 | 10| 15| 2 | 3 | 0 |
+-----------+---+---+---+---+---+
|2015-06-02 | 20| 11| 5 | 4 | 1 |
+-----------+---+---+---+---+---+
|2015-06-03 | 5 | 10| 5 | 6 | 2 |
+-----------+---+---+---+---+---+
.
.
.
+-----------+---+---+---+---+---+
|2015-06-30 | 10| 15| 2 | 3 | 0 |
+-----------+---+---+---+---+---+
然后我需要在这样的表格中显示数据。
+------------+---+---+---+---+---+
| date/eval | 5 | 4 | 3 | 2 | 1 |
+------------+---+---+---+---+---+
|june 2015 |110|105| 32| 23| 0 |
+------------+---+---+---+---+---+
|july 2015 |132| 50| 21| 13| 0 |
+------------+---+---+---+---+---+
|august 2015 |151| 55| 42| 30| 0 |
+------------+---+---+---+---+---+
我能想到的所有sql脚本都是group by
。但它只适用于按日期分组。
这是脚本:
select * from evaluation group by year,month(datetime) asc
如何计算每次评估的唯一值。
答案 0 :(得分:2)
创建数据结构并填写测试数据:
CREATE TABLE evaluation (
evaldate date NOT NULL,
grade1 int(11) NOT NULL,
grade2 int(11) NOT NULL,
grade3 int(11) NOT NULL,
grade4 int(11) NOT NULL,
grade5 int(11) NOT NULL,
PRIMARY KEY (evaldate)
);
INSERT INTO evaluation VALUES('2015-07-14', 1, 2, 3, 4, 5);
INSERT INTO evaluation VALUES('2015-07-15', 2, 2, 2, 2, 2);
INSERT INTO evaluation VALUES('2015-08-12', 5, 4, 3, 2, 1);
INSERT INTO evaluation VALUES('2015-08-13', 1, 1, 1, 1, 1);
执行您要执行的查询:
SELECT year(evaldate)
, month(evaldate)
, sum(grade1)
, sum(grade2)
, sum(grade3)
, sum(grade4)
, sum(grade5)
FROM evaluation
group
by year(evaldate)
, month(evaldate);
返回:
year(evaldate) month(evaldate) sum(grade1) sum(grade2) sum(grade3) sum(grade4) sum(grade5)
2015 7 3 4 5 6 7
2015 8 6 5 4 3 2
在我看来,这就像你要找的结果一样,对吗?