我想转换数据,Table1
正如您在 Pivot_table 中数据的第一张图片上看到的那样。我想计算成绩字段中有多少值。可以用 MySQL 中的日期类型来做到这一点吗?我可以以某种方式使用动态命名的别名,例如你可以看到它更低AS NOW()
吗?
Table1:
PK Date Subject Grade
----------------------------------------------------
1 2013-09-11 10:37:45 History A
2 2013-09-11 10:37:45 Math B
3 2013-09-11 10:37:45 Math A
4 2013-09-11 10:37:45 Biology D
5 2013-10-11 10:37:45 History B
6 2013-10-11 10:37:45 History A
7 2013-10-11 10:37:45 Math A
8 2013-10-11 10:37:45 Geography C
9 2013-10-11 10:37:45 Geography A
10 2013-10-11 10:37:45 Geography C
Pivot_table:
Subject 11-09-2013 11-10-2013
--------------------------------------
Math 2 1
History 1 2
Biology 1
Geography 3
I tried something like this:
SELECT subject,
COUNT(CASE WHEN date = CURRENT_DATE() THEN grade END) AS NOW(),
COUNT(CASE WHEN date = DATE_ADD(NOW(), INTERVAL -1 DAY) THEN grade END) AS '11-09-2013',
COUNT(CASE WHEN date = DATE_ADD(NOW(), INTERVAL -1 MONTH) AS '11-10-2013'
FROM table1
GROUP BY subject
谢谢你的帮助