我有一個單一的表所示:返回多列從與零單個表的結果值
mysql> select RefID,State,StartTime,EndTime from execReports limit 5; +--------------------------------------+-----------+---------------------+---------------------+ | RefID | State | StartTime | EndTime | +--------------------------------------+-----------+---------------------+---------------------+ | 00019a52-8480-4431-9ad2-3767c3933627 | Completed | 2016-04-18 13:45:00 | 2016-04-18 13:45:01 | | 00038a8a-995e-4cb2-a335-cb05d5b3e92d | Aborted | 2016-05-03 04:00:00 | 2016-05-03 04:00:02 | | 001013f8-0b86-456f-bd59-a7ef066e565f | Completed | 2016-04-14 03:30:00 | 2016-04-14 03:30:11 | | 001f8d23-3022-4271-bba0-200494de678a | Failed | 2016-04-30 05:00:00 | 2016-04-30 05:00:02 | | 0027ba42-1c37-4e50-a7d6-a4e24056e080 | Completed | 2016-04-18 03:45:00 | 2016-04-18 03:45:02 | +--------------------------------------+-----------+---------------------+---------------------+
我可以提取EXEC的計數與每個狀態:
mysql> select distinct State,count(StartTime) as nbExec from execReports group by State; +-----------+--------+ | State | nbExec | +-----------+--------+ | Aborted | 3 | | Completed | 14148 | | Failed | 49 | +-----------+--------+ 4 rows in set (0.02 sec)
我可以提取每週的執行計數爲:
mysql> select distinct extract(week from StartTime) as Week, count(StartTime) as nbExec from execReports group by Week; +------+--------+ | Week | nbExec | +------+--------+ | 14 | 1317 | | 15 | 3051 | | 16 | 3066 | | 17 | 3059 | | 18 | 3059 | | 19 | 652 | +------+--------+ 6 rows in set (0.01 sec)
但是我想提取一個交叉表,如:
+------+---------+-----------+--------+---------+---------+ | Week | nbExec | Completed | Failed | Running | Aborted | +------+---------+-----------+--------+---------+---------+ | 14 | 1317 | 1312 | 3 | 1 | 1 | | 15 | 3051 | 3050 | 1 | 0 | 0 | | 16 | 3066 | 3060 | 3 | 2 | 1 | | 17 | 3059 | 3058 | 0 | 1 | 0 | | 18 | 3059 | 3057 | 1 | 0 | 1 | | 19 | 652 | 652 | 0 | 0 | 0 | +------+---------+-----------+--------+---------+---------+
我被困在這幾天。任何幫助讚賞。
此致敬禮
[MySQL的數據透視表(可能的重複http://stackoverflow.com/questions/7674786/mysql-樞軸表) –
請注意,不同的是這裏沒有做任何事 – Strawberry