mysql 2计数从一个表分组

时间:2015-12-21 18:24:46

标签: mysql

我是mysql的新手,我试图从一个表中获取每年报告的所有问题类型的列表。我想得到:

2014 Network Cable 1

2015年注射失败1

2015叉车传感器2

但到目前为止我能得到的是:

enter image description here

SQL:

     SELECT DISTINCT *
FROM 
    (SELECT natureOfProblem, COUNT(*) AS year2014
    FROM problem
    WHERE dateProblemStarted < '20150101'
    GROUP BY natureOfProblem) a,
    (SELECT natureOfProblem, COUNT(*) AS year2015
    FROM problem
    WHERE dateProblemStarted > '20141231'
    GROUP BY natureOfProblem)b
GROUP BY a.natureOfProblem, b.natureOfProblem;

Table: problem

1 个答案:

答案 0 :(得分:2)

SELECT year(dateProblemStarted), natureOfProblem, count(1)
FROM problem
GROUP BY year(dateProblemStarted), natureOfProblem